Skip to content

Instantly share code, notes, and snippets.

@MattHealy
Created July 7, 2020 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattHealy/79b039bd595e06ff785f9eadc511de1a to your computer and use it in GitHub Desktop.
Save MattHealy/79b039bd595e06ff785f9eadc511de1a to your computer and use it in GitHub Desktop.
PUT a large file directly to Amazon S3
#!/usr/bin/perl
use File::Slurp; # not strictly needed, but nice to have
use Data::Dumper;
use JSON;
use LWP::UserAgent;
use HTTP::Request::Common;
my $key = '';
my $token = '';
my $propertyid = 289;
my $lifeid = 123;
my $ua = LWP::UserAgent->new();
$ua->agent('VaultGroup/1.0');
my $headers = HTTP::Headers->new();
$headers->header(Accept => "application/json");
$headers->header(Content_Type => "application/json");
$headers->header(X_Api_Key => $key);
$headers->header(Authorization => "Bearer $token");
my $url = "https://ap-southeast-2.api.vaultre.com.au/api/v1.2/properties/$propertyid/sale/$lifeid/files/upload";
#my $filename = 'BSBDirectoryMay18-266.csv';
#my $contentType = 'text/csv';
my $filename = 'mydimport-1590056519-hires.2284-IMG3724.jpg';
my $contentType = 'image/jpeg';
my $filesize = -s $filename;
my $json = encode_json({"filename" => $filename, "filesize" => $filesize, "contentType" => $contentType});
my $request = HTTP::Request->new('POST', $url, $headers, $json);
my $response = $ua->request($request);
print "Code: " . $response->code . "\n\n";
print "Response: " . $response->content . "\n\n";
if ($response->is_success && $response->code eq "201") {
my $data = decode_json($response->decoded_content);
my $upload_url = $data->{upload_url};
# if you have File::Slurp installed...
# my $fileData = read_file($filename);
# otherwise...
my $fileData;
open(FILE, "<$filename");
while (<FILE>) {
$fileData .= $_;
}
close(FILE);
my $request = HTTP::Request::Common::PUT( $upload_url , Content_Type => $contentType, Content => $fileData );
my $response = $ua->request($request);
if ($response->code == 200) {
print "File $filename loaded to S3 successfully\n";
} else {
print "File $filename could not be loaded to S3\n\n";
print $response->decoded_content;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment