简体   繁体   中英

S3 JS SDK copyObject 403 Forbidden

putObject works just fine:

var destBucket = 'DESTBUCKETNAME';
var params = {
     Body: '01110100 01100101 01110011 01110100 01100110 01101001 01101100 01100101 01101000 01100101 01110010 01100101 ',
     Bucket: destBucket, 
     Key: "tester.mp4"
};

s3.putObject(params, function(err, data) {
     if (err) console.log(err, err.stack); // an error occurred
     else     console.log(data);           // successful response
});

This uploads the file and returns a successful response.

However copyObject returns a 403 forbidden on the destination object location:

var sourceObject = 'testsrcfile.mp4';
var srcBucket = 'BUCKETNAME';
var destBucket = 'DESTBUCKETNAME';
var paramsCopy = {
     Bucket: destBucket,
     CopySource: srcBucket + '/' + sourceObject,
     Key: destBucket + '/' + 'testy.mp4' 
};
s3.copyObject(params, function(err, data) {
     if (err) console.log(err, err.stack); // an error occurred
     else     console.log(data);           // successful response
}); 

Error:

PUT https://DESTBUCKETNAME.s3.us-west-1.amazonaws.com/DESTBUCKETNAME/testy.mp4 403 (Forbidden)

Here's my CORS Config for the destination bucket:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <ExposeHeader>ETag</ExposeHeader>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

I don't understand why putObject works while copyObject does not. Thoughts?

Your are specifying the Key incorrectly. Do not specify the bucket name in the Key. That is what Bucket is for.

var paramsCopy = {
     Bucket: destBucket,
     CopySource: srcBucket + '/' + sourceObject,
     Key: 'testy.mp4' 
};

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