Skip to content

Instantly share code, notes, and snippets.

@stevelippert
Created October 12, 2016 19:22
Show Gist options
  • Save stevelippert/5ba5cb2ad7262d83be0a54e1ffbcfb8e to your computer and use it in GitHub Desktop.
Save stevelippert/5ba5cb2ad7262d83be0a54e1ffbcfb8e to your computer and use it in GitHub Desktop.
An example of uploading a local file to S3.
#!/usr/bin/perl
use warnings;
use strict;
use Amazon::S3;
use vars qw/$OWNER_ID $OWNER_DISPLAYNAME/;
my $aws_access_key_id = 'YOUR AWS ACCESS KEY HERE';
my $aws_secret_access_key = 'YOUR AWS SECRET ACCESS KEY HERE';
my $s3 = Amazon::S3->new(
{ aws_access_key_id => $aws_access_key_id,
aws_secret_access_key => $aws_secret_access_key,
retry => 1,
}
);
my $response = $s3->buckets;
# create a bucket
my $bucket_name = 'weatherstem'; #The name of the bucket to store this file in.
my $bucket = $s3->bucket($bucket_name); #Attach the $s3 object to the named bucket.
# store a key with a content-type and some optional metadata
my $keyname = 'skycamera/alachua/aes/cumulus/2016/10/12/14/05.jpg'; # This is the file name for use in the bucket.
my $local_file = '/home/ubuntu/header-alerts.jpg'; # This is the full file path of the file to upload.
$bucket->add_key_filename(
$keyname, $local_file, { content_type => 'image/jpeg',}
);
# list keys in the bucket
# I.e. list all of the files in the bucket.
$response = $bucket->list or die $s3->err . ": " . $s3->errstr;
print $response->{bucket}."\n";
for my $key (@{ $response->{keys} }) {
print "\t".$key->{key}."\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment