Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

810
Visualizações
How can I move (not copy) a file in AWS S3 using their sdk?

I need to move a file in AWS s3 bucket to another location, example:

  • From: http://aws.xxxxx/xxxx/locationA/file.png
  • To: http://aws.xxxxx/xxxx/locationB/file.png

I've looked over the documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html, but haven't found any mention of either moving nor updating the file (I'm thinking I could update the file Key path...).

So far, it seems I need to copy the file then remove the old one? Is there a more straightforward way of doing it?

My current code which copies then removes old file:

function moveFileInAws(fromLocation, toLocation, callback) {
  awsSdk.copyObject({
    Bucket: BUCKET_NAME,
    ACL: 'public-read',
    CopySource: fromLocation,
    Key: toLocation
  }, (err, data) => {
    if (err) {
        console.log(err)
        return callback("Couldn't copy files in directory")
    }
    // callback()
    awsSdk.deleteObject({ Key: fromLocation }, (err, data) => {
      if (err) {
        console.log("Couldn't delete files in directory")
        console.log(err)
        return callback("Couldn't delete files in directory")
      }
      callback()
    })
  })
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Based on the answer here: AWS S3 - Move an object to a different folder in which user @Michael-sqlbot comments:

That's because S3, itself, doesn't have an atomic move or rename operation... so there isn't really an alternative

And the docs here: https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/examples-s3-objects.html#copy-object (for the Java SDK, but seems to be useful here) which notes:

You can use copyObject with deleteObject to move or rename an object, by first copying the object to a new name (you can use the same bucket as both the source and destination) and then deleting the object from its old location.

It sounds like S3's infrastructure simply doesn't support moving or renaming in a single operation. You must copy, then delete.

over 4 years ago · Santiago Trujillo Relatório

0

Amazon S3 doesn’t provide an API to move or rename an object from one bucket to another in a single step.

As in your example, you can use copyObject with deleteObject to move or rename an object, by first copying the object to a new name (you can use the same bucket as both the source and destination) and then deleting the object from its old location.

For more information see Performing Operations on Amazon S3 Objects

over 4 years ago · Santiago Trujillo Relatório

0

I'm not at all familiar w/ the JavaScript SDK that you're using, but using aws cli, there is:

aws s3 mv s3://bucket/folder/file s3://bucket/folder2/file

that seems it would do what you want. Not sure about JavaScript SDK.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda