Aws basic commands in ruby

First create an object of s3 client through loading yml of aws_credential which looks like:
aws_credentails:
  access_key_id: access_key_id
  secret_access_key: secret_access_key

  region: region_name
  source_upload_bucket: bucket_name
  rails_bucket: rails_bucket_name
  ipad_bucket: ipad_bucket_name(if exists)
  application_arn: if_exists

name_prefix: prefix_name_of_bucket
s3_client= Aws::S3::Client.new(aws_credentials)
1.) To list all buckets:
 s3_client.list_buckets
2.) Listing objects of a particular bucket:
s3_client.list_objects(bucket:"bucket-name")
3.) Listing all key of a bucket in an array:
s3_client.list_objects({bucket: "bucket-name"})[:contents].map{|x| x.key}
4.) To delete a key from a bucket:
 s3_client.delete_object(bucket: "bucket-name", key:"10_sheet.zip_256")
5.) To copy existing directory to new directory:
def copy_existing_dir_to_new_dir(bucket_name=bucket-name)
    responce = s3.list_objects({bucket: bucket-name, max_keys: 10000000})
    responce.contents.each do |obj|
      _key = obj.key
      _target_key_values = _key.split('/')
      _target_key_values[0] = ['256', _target_key_values.first].join('-')
      _target_key = _target_key_values.join('/')
      copy_file(bucket_name, _key, _target_key)
    end
  end
6.) To check whether a bucket does exist or not:
s3_client.head_bucket({bucket: 'bucket-name'})
7.) To check whether an object does exist or not:

s3_client.head_object({
  bucket: "BucketName", # required
  key: "ObjectKey", # required
})

8.) To get an object of a bucket;
 
s3_cleint.get_object(
  bucket: 'bucket-name',
  key: 'object-key' 
)

9.) To create a bucket:

s3_client.create_bucket({
  bucket: "BucketName", # required
})

10.) To put an object in a bucket:

client.put_object({
  bucket: "BucketName", # required
  key: "ObjectKey", # required
})




Comments

Popular posts from this blog

Installing Wowza Streaming Engine on ubuntu

Highcharts with grouped categories

Completely Uninstall and Install Rubymine on ubuntu