如何使用Python直接将JSON数据(来自API调用)直接直接到S3存储键进行加载?
我对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'
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用
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
orsome-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.