如何在heroku上使用python中的boto3将文件上传到s3? [英] How do I upload a file to s3 using boto3 in python on heroku?

查看:24
本文介绍了如何在heroku上使用python中的boto3将文件上传到s3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网上所有的例子似乎都使用boto.我可以使用什么代码使用 boto3 将文件上传到 s3?

All of the examples online seem to use boto. What code can I use to upload a file to s3 using boto3?

我正在尝试使用官方文档中的代码,但收到版本 4 身份验证失败的错误.

I'm trying to use the code from the official documentation, but I'm getting an error that version 4 authentication failed.

推荐答案

根据 快速入门指南,然后使用以下代码:

Put your keys in the keyfile according to the quickstart guide and then use the following code:

       import boto3
       s3 = boto3.resource(service_name='s3', region_name='REGION_NAME')
       data = open('PATH_TO_FILE_ON_DISK', 'rb')
       s3.Bucket('BUCKET_NAME').put_object(Key='FILENAME_ON_S3', Body=data)

官方文档并没有说清楚区域名称是必需的,否则可能会报错,并且无法在heroku上存储区域的配置文件.它必须包含在资源调用中.

The official documentation does not make it clear that the region name is required or else you may get an error, and it's not possible to store the configuration file for the region on heroku. It must be included in the resource call.

这篇关于如何在heroku上使用python中的boto3将文件上传到s3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆