S3ResponseError:403禁止使用boto

时间:2013-05-01 18:49:14

标签: python amazon-web-services amazon-s3 boto

我有一个脚本可以将文件从一个S3帐户复制到另一个S3帐户,这样做很有用!!!!这是肯定的。比我今天尝试过,它不再给我错误S3ResponseError: 403 Forbidden。我100%确定凭据是正确的,我可以使用aws控制台手动从两个帐户下载密钥。

代码

def run(self):
        while True:
            # Remove and return an item from the queue
            key_name = self.q.get()
            k = Key(self.s_bucket, key_name)
            d_key = Key(self.d_bucket, k.key)
            if not d_key.exists() or k.etag != d_key.etag:
                print 'Moving {file_name} from {s_bucket} to {d_bucket}'.format(
                               file_name = k.key,
                               s_bucket = source_bucket,
                               d_bucket = dest_bucket
                )
                # Create a new key in the bucket by copying another existing key
                acl = self.s_bucket.get_acl(k)
                self.d_bucket.copy_key( d_key.key, self.s_bucket.name, k.key, storage_class=k.storage_class)
                d_key.set_acl(acl)
            else:
                print 'File exist'

            self.q.task_done()

错误:

  File "s3_to_s3.py", line 88, in run
    self.d_bucket.copy_key( d_key.key, self.s_bucket.name, k.key, storage_class=k.storage_class)
  File "/usr/lib/python2.7/dist-packages/boto/s3/bucket.py", line 689, in copy_key
    response.reason, body)
S3ResponseError: S3ResponseError: 403 Forbidden
<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>0729E8ADBD7A9E60</RequestId><HostId>PSbbWCLBtLAC9cjW+52X1fUSVErnZeN79/w7rliDgNbLIdCpc9V0bPi8xO9fp1od</HostId></Error>

1 个答案:

答案 0 :(得分:1)

尝试此操作:使用boto的Key类

将密钥从源存储桶复制到目标存储桶
source_key_name = 'image.jpg' # for example

#return Key object
source_key = source_bucket.get_key(source_key_name)          
#use Key.copy
source_key.copy(destination_bucket,source_key_name)

关于复制功能。你可以将preserve_acl设置为True,它将从源密钥中复制。

Boto的Key.copy签名:

def copy(self, dst_bucket, dst_key, metadata=None,
             reduced_redundancy=False, preserve_acl=False,
             encrypt_key=False, validate_dst_bucket=True):