[英]WindowsError: [Error 5] Access is denied using boto/python
I want to download all the files and folders in the bucket. 我想下载存储桶中的所有文件和文件夹。 This is my code 这是我的代码
conn = boto.connect_s3(AWS_ACCESS_KEY_ID,
AWS_ACCESS_KEY_SECRET)
bucket = conn.get_bucket(bucket_name)
key = boto.s3.key.Key(bucket)
key.get_contents_to_filename('path/to/folder')
error- 错误-
File "C:\Python27\lib\site-packages\boto\s3\key.py", line 1726, in get_contents_to_filename
os.remove(filename)
WindowsError: [Error 5] Access is denied: 'path/to/folder'
Please help me overcome the problem! 请帮助我克服问题!
I was having this exact problem. 我当时有这个确切的问题。 The problem is that S3 Buckets don't have directories. 问题是S3存储桶没有目录。 The file structure is flat but every key is named as if it were a path name. 文件结构是平面的,但每个键的名称都像是路径名一样。
So if you have 所以如果你有
bucket
|
|__dir1
|
|_file1
|_file2
You'll actually have keys 你实际上有钥匙
bucket/dir1/
bucket/dir1/file1
bucket/dir1/file2
For me (in a Windows OS), the get_contents_to_filename
works for file keys ( bucket/dir1/file1
) but fails for directory keys ( bucket/dir1
), which triggers os.remove(filename)
and ultimately the PermissionError. 对于我(在Windows操作系统中), get_contents_to_filename
适用于文件密钥( bucket/dir1/file1
),但os.remove(filename)
用于目录密钥( bucket/dir1
),这会触发os.remove(filename)
并最终触发PermissionError。
Instead you can try to recursively iterate through your directory structure and get_contents_to_filename for file keys and mkdir for directory keys (these end in a slash '/'). 相反,您可以尝试递归地遍历目录结构,并使用get_contents_to_filename来获取文件密钥,并使用mkdir来获取目录密钥(这些以斜杠'/'结尾)。
I Was facing the same issue with boto3. 我遇到了与boto3相同的问题。 This is what I was trying- 这就是我正在尝试的
s3.Object('<bucket>','<prefix>/<filename>').download_file('C:\myfolder')
I tried multiple things like running as an admin, giving different local paths, giving public user paths etc and nothing worked. 我尝试了多种方法,例如以管理员身份运行,提供不同的本地路径,提供公共用户路径等,但没有任何效果。 The issue was that I was providing a folder path and not a file path. 问题是我提供的是文件夹路径而不是文件路径。 So this worked in the end- 所以最终成功了
s3.Object('<bucket>','<prefix>/<filename>').download_file('C:\myfolder\<filename>')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.