简体   繁体   中英

S3ResponseError: 403 Forbidden using boto

I have a script that copy files from one S3 account to another S3 account, It was working befoure!!!! That's for sure. Than I tried it today and it doesn't any more it gives me error S3ResponseError: 403 Forbidden . I'm 100% sure credentials are correct and I can go and download keys from both accounts manualy using aws console.

Code

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()

Error:

  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>

Try this: copy key from source bucket to destination bucket using boto's Key class

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)

regarding the copy function. you can set preserve_acl to True and it will be copied from the source key.

Boto's Key.copy signature:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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