简体   繁体   中英

How do I verify the file uploaded correctly to S3 with Perl Paws?

I have uploaded a file to linode object storage (s3 compatible)

However, I need to make sure that the file uploaded is correct.

How do I verify the file uploaded using Paws or Net::Amazon::s3? these are mostly mp3 files

EDIT Im now getting "Forbidden" on ContentMD5 inside PutObject its working fine when I remove that

this is my current code

use Data::Printer;
use Path::Tiny;
use Paws;

my $service    = 'S3';
my $region     = 'ap-south-1';
my $bucketname = 'KFC';

my $file = 'MP3/hello.mp3';
my $response;
my $body;

my $s3 = Paws->service($service, 
    region => $region,
    endpoint => 'http://ap-south-1.linodeobjects.com'
);

$response = uploadfile($file);
p $response;

sub uploadfile
{
    my $arg1 = $_[0];
    my $fh = path($file);
    my $body = $fh->slurp;
    my $md5 = $fh->digest("MD5");
    $md5 = "\"" . $md5 . "\""; # eq is not true if not for this >_<
    
    $response = $s3->PutObject(
        'Bucket'        => $bucketname,
        'ACL'           => "public-read",
        'ContentType'   => 'audio/mpeg',
        'Key'           => $arg1,
        'ContentMD5'    => $md5,
        'Body'          => $body
    );
    
    print "md5  = " . $md5 . "\n";
    print "Etag = " . $response->ETag . "\n";
    if ( $response->ETag eq $md5) {
        return 1;
    } else {
        return 0;
    }
}

Can you guys tell me if this is the best way to do it?

Include the ContentMD5 header when you call Paws::S3::PutObject . See AWS's docs on How can I check the integrity of an object uploaded to Amazon S3? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM