简体   繁体   English

具有“拒绝访问”错误的Python S3 Amazon代码

[英]Python S3 Amazon Code with 'Access Denied' Error

I am trying to download a specific S3 file off a server using Python Boto and am getting "403 Forbidden" and "Access Denied" error messages. 我试图使用Python Boto从服务器下载特定的S3文件,并收到“403 Forbidden”和“Access Denied”错误消息。 It says the error is occurring at line 24 (get_contents command). 它说错误发生在第24行(get_contents命令)。 I have tried it with and without the "aws s3 cp" at the start of the source file path, received the same error message both time. 我在源文件路径的开头有和没有“aws s3 cp”尝试过它,两次都收到相同的错误信息。 My code is below, any advice would be helpful. 我的代码如下,任何建议都会有所帮助。

# Code to append csv:
import csv
import boto
from boto.s3.key import Key

keyId ="key"
sKeyId="secretkey"
srcFileName="aws s3 cp s3://...."
destFileName="C:\\Users...."
bucketName="bucket00001"
conn = boto.connect_s3(keyId,sKeyId)
bucket = conn.get_bucket(bucketName, validate = False)

#Get the Key object of the given key, in the bucket
k = Key(bucket, srcFileName)
#Get the contents of the key into a file
k.get_contents_to_filename(destFileName)
srcFileName="aws s3 cp s3://...."

This has to be a key like somefolder/somekey or somekey as string. 这必须是像somefolder/somekeysomekey作为字符串的关键。

You are providing a path or command to it. 您正在为它提供pathcommand

AWS is very vague with the errors that it outputs. AWS对其输出的错误非常模糊。 This is intentional, but it definitely doesn't help with debugging. 这是故意的,但它绝对没有帮助调试。 You are receiving an Access Denied error because the source file name you are using is not the correct path for the file. 您收到“拒绝访问”错误,因为您使用的源文件名不是文件的正确路径。

aws s3 cp

This is the CLI command to copy one or more files from a source to a destination (with either being an s3 bucket). 这是将一个或多个文件从源复制到目标的CLI命令(两者都是s3存储桶)。 This should not be apart of the source file name. 这不应该是源文件名的一部分。

s3://...

This prefix is appended to your bucket name that denotes that the path refers to an s3 object, however, this is not necessary in your source file path name when using boto3. 此前缀附加到您的存储桶名称,表示该路径引用s3对象,但是,在使用boto3时,这在源文件路径名中不是必需的。

To download an s3 file using boto3, perform the following: 要使用boto3下载s3文件,请执行以下操作:

import boto3

BUCKET_NAME = 'my-bucket' # does not include s3://
KEY = 'image.jpg' # the file you want to download

s3 = boto3.resource('s3')
s3.Bucket(BUCKET_NAME).download_file(KEY, 'image.jpg')

Documentation for this command can be found here: https://boto3.readthedocs.io/en/latest/guide/s3-example-download-file.html 可以在此处找到此命令的文档: https//boto3.readthedocs.io/en/latest/guide/s3-example-download-file.html

In general, boto3 (and any other AWS SDK's) are simply wrappers around AWS api requests. 通常,boto3(以及任何其他AWS开发工具包)只是围绕AWS api请求的包装器。 You can also use the aws cli like I mentioned earlier to download a file from s3. 您也可以像我之前提到的那样使用aws cli从s3下载文件。 That command would be: 该命令将是:

aws s3 cp s3://my-bucket/my-file.jpg C:\location\my-file.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM