Voici un exemple de code ruby pour générer des URL signées pour un emplacement particulier dans votre compartiment.
Installer aws-sdk
:
gem install aws-sdk
Créer un nouveau fichier, nommez-le comme vous voulez, je choisis shareS3files.rb
voir sur gist.github.com:
#!/usr/bin/env ruby
require 'aws-sdk'
require 'json'
#loading credentials
creds = JSON.load(File.read('secrets.json'))
creds = Aws::Credentials.new(creds['AccessKeyId'], creds['SecretAccessKey'])
#loading config
conf = JSON.load(File.read('config.json'))
# Create a new S3 object
s3 = Aws::S3::Client.new(credentials: creds, region: conf['Region'])
# Create a resource object, simpler to use
resource = Aws::S3::Resource.new(client: s3)
bucket = resource.bucket(conf['Bucket'])
# enumerate every object in a bucket, and create a pre signed url with a 5 days expiration limit
bucket.objects(prefix: 'conf['Location']').each do |obj|
puts "#{obj.key} => #{obj.object.presigned_url(:get, expires_in: 5 * 24 * 60 * 60)}"
end
Maintenant, créez deux fichiers d'informations d'identification et de configuration:
secrets.json
:
{
"AccessKeyId": "TOCHANGE",
"SecretAccessKey": "TOCHANGE"
}
config.json
:
{
"Region": "TOCHANGE",
"Bucket": "tochange",
"Location": "to/change/"
}
D'autres aws et doc rubis: