1

我有一个脚本,可以将文件从一个 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>
4

1 回答 1

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):
于 2014-01-29T22:16:43.613 回答