From b6c729c700afe9dd84ecdc799ebfab38b2ea1ce5 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sun, 12 Feb 2023 16:49:39 -0500 Subject: [PATCH] Improve s3 url parsing and use Net::Amazon::S3::Vendor --- scripts/ZoneMinder/lib/ZoneMinder/Event.pm | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm index 17b024115..68775438c 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Event.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Event.pm @@ -655,17 +655,27 @@ sub CopyTo { if ( $$NewStorage{Url} ) { my $url = $$NewStorage{Url}; $url =~ s/^(s3|s3fs):\/\///ig; - my ( $aws_id, $aws_secret, $aws_host, $aws_bucket, $subpath ) = ( $url =~ /^\s*([^:]+):([^@]+)@((https?:\/\/)?[^\/]*)\/([^\/]+)(\/.+)?\s*$/ ); + $url =~ /^\s*(?[^:]+):(?[^@]+)@(?(https?:\/\/)?[^\/]*)\/(?[^\/]+)(?\/.+)?\s*$/; + my ( $aws_id, $aws_secret, $aws_host, $aws_bucket, $subpath ) = ($+{ID},$+{SECRET}, $+{HOST}, $+{BUCKET}, $+{SUBPATH}); + $subpath = '' if !$subpath; Debug("S3 url parsed to id:$aws_id secret:$aws_secret host:$aws_host, bucket:$aws_bucket, subpath:$subpath\n from $url"); - if ( $aws_id and $aws_secret and $aws_host and $aws_bucket ) { + if ($aws_id and $aws_secret and $aws_host and $aws_bucket) { eval { require Net::Amazon::S3; + require Net::Amazon::S3::Vendor::Generic; require File::Slurp; + my $vendor = undef; + if ($aws_host) { + $vendor = Net::Amazon::S3::Vendor::Generic->new( + host=>$aws_host, + authorization_method => 'Net::Amazon::S3::Signature::V4', + ); + } my $s3 = Net::Amazon::S3->new( { aws_access_key_id => $aws_id, aws_secret_access_key => $aws_secret, - ( $aws_host ? ( host => $aws_host ) : () ), - authorization_method => 'Net::Amazon::S3::Signature::V4', + ( $vendor ? (vendor => $vendor) : ( + )), }); my $bucket = $s3->bucket($aws_bucket); if ( !$bucket ) {