Skip to content

Instantly share code, notes, and snippets.

@vireshas
Forked from roparz/aws-s3-rename.coffee
Created November 8, 2016 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vireshas/0a04de200cd4859af2f21bbf77b37e70 to your computer and use it in GitHub Desktop.
Save vireshas/0a04de200cd4859af2f21bbf77b37e70 to your computer and use it in GitHub Desktop.
Rename object with Node.js AWS S3 (copy object then delete object) with promises
config = require 'config'
s3 = require 's3'
q = require 'q'
module.export = (oldKey, newKey) ->
defer = q.defer()
params =
# you need to set the s3 bucket in the CopySource key
CopySource: "#{ config.s3.bucket }/#{ oldKey }"
Key: newKey
s3.copyObject params, (err) ->
if err then defer.reject(err)
else s3.deleteObject Key: oldKey, (err) ->
if err then defer.reject(err)
else defer.resolve()
return defer.promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment