In my Rail 4 application, I have a large number of images which are stored on S3, using paperclip.
- How can I download images from the S3 and store them locally?
- How to resize it locally stored image
- Upload a resized image in another S3 bucket without paperclip (path s3 / newbucket / images / { At: id} / {imagesize.jpg})
Attachment category:
class image & lt; ActiveRecord :: Base has_attached_file: file, styles: {thumbnail: '320x320', icon: '64x64', original: '1080x1080'} is valid_content: file, presence: correct, content_type: {content_type: / \ Aimage / /.* For the original advice, the size of the images will not be changed-the-on-the-
Okay, here you can do that there is no other option.
def download_from_s3 url_to_s3, file name Yuri = URI (url_to_s3) response = net :: HTTP.get_response (uri) File.open (filename, 'wb') {| F | F.write (response.body)} end
Here we have downloaded an image located originally given URL and it has been saved locally as a file resizing something Can be done differently (depending on whether you want to serve the downloaded file as Paperclip
attachment). The most common method here is to use the ImageMagic
and its convert
command line script. Here is an example of shaping an image in the width of 30
:
convert-strip -ometree30-quality100-parpen1'/photo/assiimage/ 000 / 000/015 / original / index Jpg '' /photos/aws_images/000/000/015/original/S_30_WIDTH__q_100__index.jpg '2 & gt; And 1> / Dev / null
You can get documentation for convert
, it is not only suitable for image recycling but also image formats, blurring, crop and It is also suitable to change between much more! Besides, you may be intrested in something which seems a little old, but how to size the images using in some such information, changed
.
The last step is to resize some image to upload the image S3 bucket
I think you already have the aws-sdk
gem and < Code> AWS :: S3 example found (documents may be found).
def bucket_name, key, upload_to_s3 file S3 = AWS :: S3.new (: access_key_id => 'YOUR_ACCESS_KEY_ID': secret_access_key => 'YOUR_SECRET_ACCESS_KEY') bucket = s3. Buckets [bucket_name] obj = bucket.objects [key] obj.write (File.open (file, 'rb') ,: acl = & gt; public_read) end
Here you provide your bucket name and desired key to communicate with a received AWS :: S3
object, S3
server and basically with one option Image uploads make it visible to everyone on the web For. Note that there are plenty of additional upload options (file encryption, access permissions, metadata and more).
No comments:
Post a Comment