admin管理员组

文章数量:1316974

I am trying to copy a file from one folder to another folder in same bucket, But I am gettnin Access denined error. But if I try to do it on two different buckets means its wokring fine.

Please find what I have tried so far below:

var AWS = require('aws-sdk');
AWS.config.update({
     accessKeyId: 'xxx',
     secretAccessKey: 'xxx'
    });
var s3 = new AWS.S3();
var params = {
    Bucket : 'bucketname', /* Another bucket working fine */ 
    CopySource : 'bucketname/externall/1.txt', /* required */
    Key : "1.txt", /* required */
    ACL : 'public-read',
};
s3.copyObject(params, function(err, data) {
    if (err)
        console.log(err, err); // an error occurred
    else {
        console.log(data); // successful response
    }
});

I am trying to copy a file from one folder to another folder in same bucket, But I am gettnin Access denined error. But if I try to do it on two different buckets means its wokring fine.

Please find what I have tried so far below:

var AWS = require('aws-sdk');
AWS.config.update({
     accessKeyId: 'xxx',
     secretAccessKey: 'xxx'
    });
var s3 = new AWS.S3();
var params = {
    Bucket : 'bucketname', /* Another bucket working fine */ 
    CopySource : 'bucketname/externall/1.txt', /* required */
    Key : "1.txt", /* required */
    ACL : 'public-read',
};
s3.copyObject(params, function(err, data) {
    if (err)
        console.log(err, err); // an error occurred
    else {
        console.log(data); // successful response
    }
});
Share Improve this question edited Sep 7, 2015 at 12:27 Kishore Indraganti asked Sep 7, 2015 at 8:12 Kishore IndragantiKishore Indraganti 1,3223 gold badges18 silver badges34 bronze badges 3
  • Have you tried with different ACL, like 'authenticated-read'? – Maksim Luzik Commented Sep 7, 2015 at 8:38
  • 1 I did try your code and it is working, I was able to copy the files inside the same bucket. Is it possible that you don't have update/delete rights on your Bucket, just list and view? – Liviu Costea Commented Sep 8, 2015 at 6:54
  • I too want to copy the file in folder in my bucket to another folder in my bucket. The way i understood is that Bucket: bucketname CopySource: bucketname/sourcefolder/sourcefilename Key: Dest File name where should I specify the destination folder name? i checked this link: docs.aws.amazon./AWSJavaScriptSDK/latest/AWS/… but was not clear. – Manik Mittal Commented Oct 8, 2015 at 13:54
Add a ment  | 

2 Answers 2

Reset to default 3
var AWS = require('aws-sdk');
AWS.config.update({
    accessKeyId: 'ACCESS_KEY',
    secretAccessKey: 'SECRET_KEY',
    region: 'REGION'
});
var s3 = new AWS.S3();
var bktName = 'BUCKET_NAME';
var options = {
    Bucket: bktName,
    Prefix: 'SOURCE_FOLDER/'
};
s3.listObjectsV2(options, function (err, data) {
    if (err) {
        console.log(err);
    } else {
        data['Contents'].forEach(function (obj) {
            var lockey = obj.Key.replace(/SOURCE/g, 'TARGET');
            // Example if you want to move from /test/123/ to /test/234/ (or 123/ to 234/) then SOURCE = 123 and TARGET = 234
            var params = {
                Bucket: bktName,
                CopySource: '/' + bktName + '/' + obj.Key,
                Key: lockey
            };
            s3.copyObject(params, function (err, data) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('Inserted', lockey);
                }
            });
        });
    }
});

I used same method copyObject and used same bucket name in source and destination path, it worked. below is my code sample

{
    Bucket: bucketName,
    CopySource: '/'+bucketName+'/local/Country.png',
    Key: 'local/copy-Country.png'
}

本文标签: javascriptCopying files from one folder to another folder in s3 of same bucket NODE JSStack Overflow