问题描述
还有其他一些这样的问题,但是似乎没有一个问题能具体回答我的问题.我有一个充满要上传到Amazon S3的文件的文件夹,并且我尝试了各种不同的操作,所有这些均导致没有上传.这是我现在拥有的代码.它在S3上成功创建了文件夹,但没有上传.
There have been a few other questions like this, but none seem to have answered my problem specifically. I have a folder full of files I want to upload to Amazon S3, and I have tried various different things, all leading to no upload. Here's the code I have now. It successfully creates the folder on S3, but doesn't upload.
import os
import boto3
s3_bucket = 'bucketname'
s3_bucket_region = 'us-east-1'
folder = 'FolderName'
key_name = folder + '/'
s3_connect = boto3.client('s3', s3_bucket_region)
try:
bucket = s3_connect.put_object(Bucket=s3_bucket, Key=key_name)
print "Bucket:", bucket
except Exception as e:
print "Bucket Error " , e
# upload File to S3
for filename in os.listdir(folder):
file_key_name = folder + '/' + filename
local_path = os.getcwd()
local_name = local_path + '/' + key_name + filename
upload = s3_connect.upload_file(local_name, s3_bucket, file_key_name)
我尝试了一个类似"FolderName/"以及"FolderName/FileName"的文件名.我以为起初,第一个参数(local_name)不需要完整路径,但是相对路径会起作用,但是完整路径或相对路径都不会起作用.
I have tried a file_key_name of just something like 'FolderName/' as well as 'FolderName/FileName'. I thought at first the first argument (local_name) didn't need a full path, but a relative path would work, but neither a full or relative path works.
我想念什么?该部门非常缺乏文档.此外,它不会引发错误,只是不会上传.
What am I missing? The docs are pretty woefully lacking in this department. Plus it doesn't throw an error, it just doesn't upload.
推荐答案
您的代码对我来说非常好,但是您需要删除在Amazon S3上创建文件夹的try/catch部分.
Your code works perfectly fine for me, but you need to remove the try/catch section that creates the folder on Amazon S3.
Amazon S3实际上不支持文件夹.相反,对象的Key
包括完整路径(例如images/foo.jpg
).因此,您无需在文件夹中存储文件之前创建文件夹(因为它们不存在!).
Amazon S3 does not actually support folders. Rather, the Key
of an object includes the full path (eg images/foo.jpg
). Therefore, you don't need to create the folders before storing a file in them (since they do not exist!).
您的代码将无法使用嵌套文件夹.您可能会考虑在 AWS命令行界面(CLI)中使用aws s3 sync
或aws s3 cp
命令如果不需要用自己的代码编写.
Your code will have difficulty with nested folders. You might consider using the aws s3 sync
or aws s3 cp
commands in the AWS Command-Line Interface (CLI) if you don't need it written in your own code.
这篇关于将充满文件的文件夹上载到Amazon S3中的特定文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!