-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy paths3_sample.rb
executable file
·74 lines (65 loc) · 2.89 KB
/
s3_sample.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env ruby
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
require 'rubygems'
require 'bundler/setup'
require 'aws-sdk'
require 'uuid'
# Instantiate a new client for Amazon Simple Storage Service (S3). With no
# parameters or configuration, the AWS SDK for Ruby will look for access keys
# and region in these environment variables:
#
# AWS_ACCESS_KEY_ID='...'
# AWS_SECRET_ACCESS_KEY='...'
# AWS_REGION='...'
#
# For more information about this interface to Amazon S3, see:
# http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Resource.html
s3 = Aws::S3::Resource.new(region: 'us-west-2')
# Everything uploaded to Amazon S3 must belong to a bucket. These buckets are
# in the global namespace, and must have a unique name.
#
# For more information about bucket name restrictions, see:
# http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
uuid = UUID.new
bucket_name = "ruby-sdk-sample-#{uuid.generate}"
bucket = s3.bucket(bucket_name)
bucket.create
# Files in Amazon S3 are called "objects" and are stored in buckets. A specific
# object is referred to by its key (i.e., name) and holds data. Here, we construct
# a reference to a new object with the key "ruby_sample_key.txt" and then
# "put" the object with the body "Hello World!".
#
# For more information on Aws::S3::Object#put, see:
# http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Object.html#put-instance_method
object = bucket.object('ruby_sample_key.txt')
object.put(body: "Hello World!")
# Aws::S3::Object#public_url generates an un-authenticated URL for the object.
#
# See: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Object.html#public_url-instance_method
puts "Created an object in S3 at:"
puts object.public_url
# Generate a URL for downloading this object without using credentials or
# modifying the object's permissions. This is called a presigned URL.
#
# See: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Object.html#presigned_url-instance_method
puts "\nUse this URL to download the file:"
puts object.presigned_url(:get)
puts "(press any key to delete both the bucket and the object)"
$stdin.getc
# Aws::S3::Bucket#delete! will delete all objects within the bucket and then
# delete the bucket itself. It is equivalent to a #clear! followed by a #delete.
#
# See: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Bucket.html#delete%21-instance_method
puts "Deleting bucket #{bucket_name}"
bucket.delete!