Getting AWS::S3::S3Object from object id

119 views
Skip to first unread message

L

unread,
Mar 3, 2017, 9:01:29 PM3/3/17
to Shrine
I am trying to use the S3Object.url_for method to email the uploaded link to users. Giver an object id e.g. image.id, how can one get the S3Object instance of that object? 
I spent over 3 hours fiddling with the Shrine.storages[:store].bucket and  Shrine.storages[:store].bucket(id) without much success as for some strange reason the returned object of bucket.object would always be S3ObjectSummary instead of S3Object. Any help would be greatly appreciated.

Janko Marohnić

unread,
Mar 4, 2017, 5:20:21 AM3/4/17
to L, Shrine
Just to verify, Shrine::Storage::S3 uses aws-sdk v2, so it's not `S3Object` anymore but `Aws::S3::Object`. Also the aws-sdk v2 doesn't have `#url_for` method, it only has `#presigned_url` and `#public_url`.

If you pull in Shrine from the master branch, the Shrine::Storage::S3#object which was "protected" before is now public, so you can call it on `image.storage` (it's simpler than `Shrine.storages[:store]`):

  image.storage #=> #<Shrine::Storage::S3:...>
  image.storage.object(image.id)

If you don't want to pull Shrine from master, you can access the Aws::S3::Object through the bucket directly. Then you just have the add the S3 prefix if you're using it in your storage:

  image.storage.bucket.object([*image.storage.prefix, image.id].join("/"))

However, considering that `image.url` will call `#presigned_url` by default, and `#public_url` if you call `image.url(public: true)`, and you can pass any additional options which will be forwarded to those methods, you shouldn't ever have to use `Aws::S3::Object` directly for generating URLs.

Btw, aws-sdk v2 also has `Aws::S3::ObjectSummary`, but when I call `image.storage.bucket.object(...)` I get a `Aws::S3::Object` back, so I cannot reproduce the behaviour you're experiencing. I don't know what does aws-sdk use `Aws::S3::ObjectSummary` for, but it seems to have the same methods as `Aws::S3::Object`.

Kind regards,
Janko

On Sat, Mar 4, 2017 at 1:01 PM, L <hitech...@gmail.com> wrote:
I am trying to use the S3Object.url_for method to email the uploaded link to users. Giver an object id e.g. image.id, how can one get the S3Object instance of that object? 
I spent over 3 hours fiddling with the Shrine.storages[:store].bucket and  Shrine.storages[:store].bucket(id) without much success as for some strange reason the returned object of bucket.object would always be S3ObjectSummary instead of S3Object. Any help would be greatly appreciated.

--
You received this message because you are subscribed to the Google Groups "Shrine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-shrine+unsubscribe@googlegroups.com.
To post to this group, send email to ruby-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-shrine/6323ea7c-e829-4c12-bd77-b8cd3195abf7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages