Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

805
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda