3

Does Shrine support a way to copy/move files between folders inside a S3 Bucket?

For instance, I upload a file into a folder called cache and if everything is OK, I then move that file into a store folder and clear the cache (immediately or using a background task).

The cache and store are different Shrine stores (although they belong to the same bucket).

jpac
  • 69
  • 5

1 Answers1

2

Shrine automatically performs a copy request when uploading a file that's uploaded to S3.

Shrine.storages = {
  cache: Shrine::Storage::S3.new(...),
  store: Shrine::Storage::S3.new(...),
}

cached_file = Shrine.upload(file,        :cache) # performs a `put_object` operation
stored_file = Shrine.upload(cached_file, :store) # performs a `copy_object` operation

So, in the attachment flow, when Shrine "promotes" the cached file to permanent storage, internally the S3 storage will make a copy request.

Since S3 doesn't support moving objects, you can simply delete the cached file after copying.

Janko
  • 8,985
  • 7
  • 34
  • 51
  • what options does `Shrine.upload` receives (besides `io` and `store`)? Can I define a different path to copy the file (instead of the root or the cached file's)? – jpac Nov 06 '19 at 18:00
  • 1
    Yes, you can pass the destination path to a `:location` keyword argument. – Janko Nov 07 '19 at 19:25