node.js通过aws sdk模块重命名s3对象

node.js通过aws sdk模块重命名s3对象,node.js,aws-sdk,Node.js,Aws Sdk,是否可以通过重命名s3上的对象?我找不到解决这个问题的方法,也许有一个临时解决方案…我想我会回答,因为没有人这样做-这个应该可以 // create a new s3 object var s3 = new AWS.S3(); var BUCKET_NAME = 'your-bucket-name'; var OLD_KEY = '/original-file.js'; var NEW_KEY = '/new-file.js'; // Copy the object to a new loc

是否可以通过重命名s3上的对象?我找不到解决这个问题的方法,也许有一个临时解决方案…

我想我会回答,因为没有人这样做-这个应该可以

// create a new s3 object
var s3 = new AWS.S3();

var BUCKET_NAME = 'your-bucket-name';
var OLD_KEY = '/original-file.js';
var NEW_KEY = '/new-file.js';

// Copy the object to a new location
s3.copyObject({
  Bucket: BUCKET_NAME, 
  CopySource: `${BUCKET_NAME}${OLD_KEY}`, 
  Key: NEW_KEY
 })
  .promise()
  .then(() => 
    // Delete the old object
    s3.deleteObject({
      Bucket: BUCKET_NAME, 
      Key: OLD_KEY
    }).promise()
   )
  // Error handling is left up to reader
  .catch((e) => console.error(e))

这只是@nf071590答案的一个流程。太棒了

下面,获取一个bucket的完整列表,然后将不是
.jpg
的每个图像的图像名称更改为
.jpg

希望这对某人有帮助。:)


除非它们已更改,否则我认为您无法重命名S3上的对象。您可以将其复制到一个具有新名称的对象,然后删除原始对象。下面是一个示例:@barry johnson:我希望不是这样@jarmod:我以前知道回购,但我不想删除或复制该对象,事实就是这样;您将需要复制和删除。
const start = new Date()
const AWS = require('aws-sdk')
const state = {}

AWS.config.update({ region: 'ADD_REGION_HERE' })

try {
    var s3 = new AWS.S3();

    var BUCKET_NAME = 'ADD_BUCKET_NAME_HERE';

    var params = {
      Bucket: BUCKET_NAME,
      MaxKeys: 1000
    };
    s3.listObjects(params, function (err, data) {
      if (err) {
        console.log(err, err.stack); // an error occurred
      } else {
        console.log(data);
        data.Contents.forEach(image => {

          var OLD_KEY = image.Key
          var NEW_KEY = ''
          // split key
          var keyArray = image.Key.split('.')
          var keyArrayLength = keyArray.length
          console.log(keyArrayLength);
          var ext = keyArray[keyArrayLength - 1]
          // console.log(ext);
          if(ext != 'jpg') {
            console.log('Change this ext FROM: ', OLD_KEY)
            ext = 'jpg'
            if (keyArrayLength == 2) {
              NEW_KEY = `${keyArray[0]}.${ext}`
            } else if (keyArrayLength == 3) {
              NEW_KEY = `${keyArray[0]}.${keyArray[1]}.${ext}`
            } else if (keyArrayLength == 4) {
              NEW_KEY = `${keyArray[0]}.${keyArray[1]}.${keyArray[2]}.${ext}`
            }
            console.log('TO:: ', NEW_KEY);
            // Copy the object to a new location
            try {
              s3.copyObject({
                Bucket: BUCKET_NAME,
                CopySource: `${BUCKET_NAME}/${OLD_KEY}`,
                Key: NEW_KEY
              }).promise()
                .then((response) => {
                  console.log('Seemed to have worked??');
                  console.log(response);
                  // Delete the old object
                  s3.deleteObject({
                    Bucket: BUCKET_NAME,
                    Key: OLD_KEY
                  }).promise()
                })
                // Error handling is left up to reader
                .catch((e) => console.error(e))
            } catch (error) {
              console.log('error::', error);
            }
          }
        });
      } 
    });
} catch (err) {
  const end = new Date() - start
  let seconds = end / 1000
  state.seconds = seconds
  state.error = err
  state.status = "error"
  state.message = err.message
  console.log(err)
  console.log(state);
  return
}