如何使用 boto 将文件上传到 S3 存储桶中的目录

如何使用 boto 将文件上传到 S3 存储桶中的目录

<p>我想使用 python 复制 s3 存储桶中的文件。<br /> 例如:我有存储桶名称 = 测试。 在存储桶中,我有 2 个文件夹,名称为“转储”和“输入”。 现在我想使用 python 将文件从本地目录复制到 S3 “转储”文件夹…有谁可以帮助我吗?</p>
如何使用 boto 将文件上传到 S3 存储桶中的目录 2022-09-01 15:09:52
如何使用 boto 将文件上传到 S3 存储桶中的目录 0
如何使用 boto 将文件上传到 S3 存储桶中的目录
<p>试试这样…</p> <pre><code class="lang-">import boto import boto.s3 import sys from boto.s3.key import Key </code></pre> <pre><code class="lang-">AWS_ACCESS_KEY_ID = '' AWS_SECRET_ACCESS_KEY = '' bucket_name = AWS_ACCESS_KEY_ID.lower() + '-dump' conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) </code></pre> <pre><code class="lang-">bucket = conn.create_bucket(bucket_name, location=boto.s3.connection.Location.DEFAULT) testfile = &quot;replace this with an actual filename&quot; print 'Uploading %s to Amazon S3 bucket %s' % \ (testfile, bucket_name) def percent_cb(complete, total): sys.stdout.write('.') sys.stdout.flush() k = Key(bucket) k.key = 'my test file' k.set_contents_from_filename(testfile, cb=percent_cb, num_cb=10) </code></pre> <p>[更新] 我不是预言家,所以感谢你对导入语句的提醒。此外,我不建议将凭据放在您自己的源代码中。<br /> 如果您在 Amazon 内部运行它,请使用 IAM 凭证和实例配置文件 <a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html" target="_blank">http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html</a><br /> ,在你的开发/测试环境,请使用 AdRoll 的 Hologram 之类的东西 <a href="https://github.com/AdRoll/hologram" target="_blank">https://github.com/AdRoll/hologram</a></p>