Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Humoud/4930ebc26166f18d24cdf203ec1b68bb to your computer and use it in GitHub Desktop.
Save Humoud/4930ebc26166f18d24cdf203ec1b68bb to your computer and use it in GitHub Desktop.

Revisions

  1. Humoud renamed this gist Jul 3, 2017. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions upload-images-s3-sdk-ruby → upload-images-s3-sdk-ruby.rb
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    Using gem aws-sdk for a ror application for uploading images to s3
    Uploading images to a fixed bucket with different folders for each object or application.
    The s3 keeps a limitation on the number of buckets creattion whereas there is no
    limitation for content inside a bucket.
    #Using gem aws-sdk for a ror application for uploading images to s3
    #Uploading images to a fixed bucket with different folders for each object or application.
    #The s3 keeps a limitation on the number of buckets creattion whereas there is no
    #limitation for content inside a bucket.

    This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public
    so that the images uploaded are directly accessible. The input it takes is the image complete path
    where it is present, folder in which it should be uploaded and user_id for whom it should
    be uploaded.
    #This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public
    #so that the images uploaded are directly accessible. The input it takes is the image complete path
    #where it is present, folder in which it should be uploaded and user_id for whom it should
    #be uploaded.

    def save_screenshot_to_s3(image_location, folder_name,user_id)
    service = AWS::S3.new(:access_key_id => ACCESS_KEY_ID,
  2. @Bijendra Bijendra revised this gist Jul 14, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion upload-images-s3-sdk-ruby
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ The s3 keeps a limitation on the number of buckets creattion whereas there is no
    limitation for content inside a bucket.

    This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public
    so that the images uploaded are directly accessible. The input is takes is the image complete path
    so that the images uploaded are directly accessible. The input it takes is the image complete path
    where it is present, folder in which it should be uploaded and user_id for whom it should
    be uploaded.

  3. @Bijendra Bijendra created this gist Feb 7, 2014.
    27 changes: 27 additions & 0 deletions upload-images-s3-sdk-ruby
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    Using gem aws-sdk for a ror application for uploading images to s3
    Uploading images to a fixed bucket with different folders for each object or application.
    The s3 keeps a limitation on the number of buckets creattion whereas there is no
    limitation for content inside a bucket.

    This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public
    so that the images uploaded are directly accessible. The input is takes is the image complete path
    where it is present, folder in which it should be uploaded and user_id for whom it should
    be uploaded.

    def save_screenshot_to_s3(image_location, folder_name,user_id)
    service = AWS::S3.new(:access_key_id => ACCESS_KEY_ID,
    :secret_access_key => SECRET_ACCESS_KEY)
    bucket_name = "app-images"
    if(service.buckets.include?(bucket_name))
    bucket = service.buckets[bucket_name]
    else
    bucket = service.buckets.create(bucket_name)
    end
    bucket.acl = :public_read
    key = folder_name.to_s + "/" + File.basename(image_location)
    s3_file = service.buckets[bucket_name].objects[key].write(:file => image_location)
    s3_file.acl = :public_read
    user = User.where(id: user_id).first
    user.image = s3_file.public_url.to_s
    user.save
    end