如何使用Python直接将JSON数据(来自API调用)直接直接到S3存储键进行加载?

发布于 2025-01-31 11:04:13 字数 747 浏览 3 评论 0 原文

我对AWS s3 的新事物相对较新,我正在调用API,将JSON数据直接加载到 s3 bucket。 S3 存储桶数据将通过 snowflake 读取。研究后,我发现使用 boto3 我们可以将数据直接加载到 s3 中。代码看起来像下面的东西,但是我不确定的一件事是我应该为密钥放置什么,因为我的 s3 bucket中没有创建对象。另外,将JSON数据加载到 s3 的好习惯是什么?我是否需要将JSON数据编码为完成的'utf-8' A>由SO用户UWE Bretschneider。

提前致谢!

Python代码:

import json,urllib.request
import boto3
data = urllib.request.urlopen("https://api.github.com/users?since=100").read()
output = json.loads(data) 
print (output) #Checking the data


s3 = boto3.client('s3')
s3.put_object(
     Body=str(json.dumps(data))
     Bucket='I_HAVE_BUCKET_NAME'
     Key='your_key_here'
)

I am relatively new to AWS s3 I am calling an API to load the JSON data directly to s3 bucket. From s3 bucket data will be read by Snowflake. After researching I found that using Boto3 we can load data into s3 directly. Code will look something like below, however one thing I am not sure about is What should I put for the key as there is no object created in my S3 bucket. Also, what is the good practice to load the JSON data to s3 ? Do I need to encode JSON data to 'UTF-8' as done here by SO user Uwe Bretschneider.

Thanks in advance!

Python code:

import json,urllib.request
import boto3
data = urllib.request.urlopen("https://api.github.com/users?since=100").read()
output = json.loads(data) 
print (output) #Checking the data


s3 = boto3.client('s3')
s3.put_object(
     Body=str(json.dumps(data))
     Bucket='I_HAVE_BUCKET_NAME'
     Key='your_key_here'
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追我者格杀勿论 2025-02-07 11:04:13

通过使用 put_object ,这意味着您在存储桶中创建一个新对象,因此没有现有密钥。
此键就像文件系统中的文件名一样。您可以指定自己喜欢的名称,例如 my-data.json some-dir/my-data.json 。您可以在

至于编码器,最好指定编码IMO,只是为了确保您的源文件也正确编码。

By using put_object, which means you are creating a new object in the bucket, so there is no existing key.
This key is just like a file name in the file system. You can specify whatever names you like, such as my-data.json or some-dir/my-data.json. You can find out more in https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html.

As for encoder, it's always good to specify the encoding IMO, just to make sure your source file has properly encoded too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文