Python S3 亚马逊代码 'Access Denied' 错误
Python S3 Amazon Code with 'Access Denied' Error
我正在尝试使用 Python Boto 从服务器下载特定的 S3 文件,但收到“403 Forbidden”和 "Access Denied" 错误消息。它说错误发生在第 24 行(get_contents 命令)。我已经尝试过在源文件路径的开头使用和不使用 "aws s3 cp",两次都收到相同的错误消息。我的代码如下,任何建议都会有所帮助。
# 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://...."
这必须是像 somefolder/somekey
或 somekey
作为字符串的键。
您正在为其提供 path
或 command
。
AWS 对其输出的错误非常模糊。这是故意的,但绝对无助于调试。您收到 拒绝访问 错误,因为您使用的源文件名不是文件的正确路径。
aws s3 cp
这是 CLI 命令,用于将一个或多个文件从源复制到目标(其中一个是 s3 存储桶)。这不应与源文件名分开。
s3://...
此前缀附加到您的存储桶名称,表示该路径引用 s3 对象,但是,使用 boto3 时,这在您的源文件路径名中不是必需的。
要使用 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')
可在此处找到此命令的文档:
https://boto3.readthedocs.io/en/latest/guide/s3-example-download-file.html
一般来说,boto3(以及任何其他 AWS SDK)只是 AWS api 请求的包装器。您还可以像我之前提到的那样使用 aws cli 从 s3 下载文件。该命令将是:
aws s3 cp s3://my-bucket/my-file.jpg C:\location\my-file.jpg
我正在尝试使用 Python Boto 从服务器下载特定的 S3 文件,但收到“403 Forbidden”和 "Access Denied" 错误消息。它说错误发生在第 24 行(get_contents 命令)。我已经尝试过在源文件路径的开头使用和不使用 "aws s3 cp",两次都收到相同的错误消息。我的代码如下,任何建议都会有所帮助。
# 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://...."
这必须是像 somefolder/somekey
或 somekey
作为字符串的键。
您正在为其提供 path
或 command
。
AWS 对其输出的错误非常模糊。这是故意的,但绝对无助于调试。您收到 拒绝访问 错误,因为您使用的源文件名不是文件的正确路径。
aws s3 cp
这是 CLI 命令,用于将一个或多个文件从源复制到目标(其中一个是 s3 存储桶)。这不应与源文件名分开。
s3://...
此前缀附加到您的存储桶名称,表示该路径引用 s3 对象,但是,使用 boto3 时,这在您的源文件路径名中不是必需的。
要使用 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')
可在此处找到此命令的文档: https://boto3.readthedocs.io/en/latest/guide/s3-example-download-file.html
一般来说,boto3(以及任何其他 AWS SDK)只是 AWS api 请求的包装器。您还可以像我之前提到的那样使用 aws cli 从 s3 下载文件。该命令将是:
aws s3 cp s3://my-bucket/my-file.jpg C:\location\my-file.jpg