StorageClient/Blob.pm untested

filter: Statementstmt 0.0% 0 / 840 Branchbran 0.0% 0 / 370 Conditioncond 0.0% 0 / 102 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 67 Podpod 0.0% 0 / 51 totaltotal 0.0% 0 / 1430 CC 186 SCAR 57.6
CC
186
Cov
0%
download
1056.0
upload
930.0
_get
272.0
CRAP 316.1
1 package Net::Azure::StorageClient::Blob;
2 use base qw/Net::Azure::StorageClient/;
3 use strict;
4 use warnings;
5 {
6 $Net::Azure::StorageClient::Blob::VERSION = '0.92';
7 }
8 use File::Spec;
9 use XML::Simple;
10 use Digest::MD5;
11 use Encode;
12 use File::Basename;
13  
14 sub init {
15 my $blobService = shift;
16 my %args = @_;
17 $blobService->SUPER::init( @_ );
18 my $container_name = $args{ container_name };
19 if ( $container_name ) {
20 $container_name =~ s!/!!g;
21 $blobService->{ container_name } = $container_name;
22 }
23 $blobService->{ type } = 'blob';
24 return $blobService;
25 }
26  
27 sub list_containers {
28 my $blobService = shift;
29 my ( $params ) = @_;
30 return $blobService->list( '', $params );
31 }
32  
33 sub set_blob_service_properties {
34 my $blobService = shift;
35 my ( $params ) = @_;
36 my $prop = $blobService->get_blob_service_properties( $params );
37 if ( $prop->code != 200 ) {
38 return $prop;
39 }
40 my $result = $prop->content;
41 my $xml = XML::Simple->new;
42 my $list = $xml->XMLin( $result );
43 my $properties = $params->{ StorageServicePropertie };
44 my @properties_Logging = qw/ Version Delete Write Read /;
45 my @properties_Metrics = qw/ Version Enabled IncludeAPIs /;
46 for my $prop( @properties_Logging ) {
47 $properties->{ Logging }->{ $prop } = $list->{ Logging }->{ $prop }
48 if ( (! $properties->{ Logging }->{ $prop } )
49 && ( $list->{ Logging }->{ $prop } ) );
50 }
51 $properties->{ Logging }->{ 'RetentionPolicy' } = $list->{ Logging }->{ RetentionPolicy }
52 unless $properties->{ Logging }->{ RetentionPolicy } && $list->{ Logging }->{ RetentionPolicy };
53 for my $prop( @properties_Metrics ) {
54 $properties->{ Metrics }->{ $prop } = $list->{ Metrics }->{ $prop }
55 if ( (! $properties->{ Metrics }->{ $prop } )
56 && ( $list->{ Metrics }->{ $prop } ) );
57 }
58 $properties->{ Metrics }->{ 'RetentionPolicy' } = $list->{ Metrics }->{ RetentionPolicy }
59 unless $properties->{ Metrics }->{ RetentionPolicy } && $list->{ Metrics }->{ RetentionPolicy };
60 my $DefaultServiceVersion;
61 if (! $properties->{ DefaultServiceVersion } ) {
62 $properties->{ DefaultServiceVersion } = $list->{ DefaultServiceVersion }
63 if $list->{ DefaultServiceVersion };
64 }
65 my $body = $xml->XMLout( $properties, NoAttr => 1, RootName => 'StorageServiceProperties' );
66 $body = '<?xml version="1.0" encoding="utf-8"?>' . "\n${body}";
67 my $data = '?restype=service&comp=properties';
68 my $options = $params->{ options };
69 $data .= '&' . $options if $options;
70 $params->{ body } = $body;
71 return $blobService->put( $data, $params );
72 }
73  
74 sub get_blob_service_properties {
75 my $blobService = shift;
76 my ( $params ) = @_;
77 my $data = '?restype=service&comp=properties';
78 my $options = $params->{ options };
79 $data .= '&' . $options if $options;
80 return $blobService->get( $data, $params );
81 }
82  
83 sub create_container {
84 my $blobService = shift;
85 my ( $name, $params ) = @_;
86 $name =~ s!^/!!;
87 my $data = 'restype=container';
88 my $options = $params->{ options };
89 $data .= '&' . $options if $options;
90 my $path = "${name}?${data}";
91 if ( my $public_access = $params->{ public_access } ) {
92 if ( $public_access !~ /^blob|container$/ ) {
93 $public_access = 'container';
94 }
95 $params->{ headers }->{ 'x-ms-blob-public-access' } = $public_access;
96 }
97 $params->{ body } = $data;
98 return $blobService->put( $path, $params );
99 }
100  
101 sub get_container_properties {
102 my $blobService = shift;
103 return $blobService->get_properties( @_ );
104 }
105  
106 sub get_container_metadata {
107 my $blobService = shift;
108 return $blobService->get_metadata( @_ );
109 }
110  
111 sub set_container_metadata {
112 my $blobService = shift;
113 return $blobService->set_metadata( @_ );
114 }
115  
116 sub get_container_acl {
117 my $blobService = shift;
118 my ( $name, $params ) = @_;
119 $name =~ s!^/!!;
120 $name .= '?restype=container&comp=acl';
121 my $options = $params->{ options };
122 $name .= '&' . $options if $options;
123 return $blobService->get( $name, $params );
124 }
125  
126 sub set_container_acl {
127 my $blobService = shift;
128 my ( $name, $params ) = @_;
129 $name =~ s!^/!!;
130 $name .= '?restype=container&comp=acl';
131 my $options = $params->{ options };
132 $name .= '&' . $options if $options; # timeout=n
133 if ( my $public_access = $params->{ public_access } ) {
134 if ( $public_access !~ /^blob|container$/ ) {
135 $public_access = 'container';
136 }
137 $params->{ headers }->{ 'x-ms-blob-public-access' } = $public_access;
138 }
139 my $Permission = $params->{ Permission } || 'rwdl';
140 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime( time );
141 my $ts = sprintf( "%04d-%02d-%02d", $year + 1900, $mon + 1, $mday );
142 my $id = $blobService->_signed_identifier( 64 );
143 my $SignedIdentifiers = { SignedIdentifier => { Id => $id,
144 AccessPolicy => { Start => $ts,
145 Expiry => $ts,
146 Permission => $Permission }, }, };
147 my $xml = XML::Simple->new;
148 my $body = $xml->XMLout( $SignedIdentifiers, NoAttr => 1, RootName => 'SignedIdentifiers' );
149 $body = '<?xml version="1.0" encoding="utf-8"?>' . "\n${body}";
150 $params->{ body } = $body;
151 return $blobService->put( $name, $params );
152 }
153  
154 sub delete_container {
155 my $blobService = shift;
156 my ( $name, $params ) = @_;
157 $name =~ s!^/!!;
158 my $data = 'restype=container';
159 my $options = $params->{ options };
160 $data .= '&' . $options if $options;
161 my $path = "${name}?${data}";
162 return $blobService->delete( $path, $params );
163 }
164  
165 sub lease_container {
166 my $blobService = shift;
167 return $blobService->lease( @_ );
168 }
169  
170 sub list_blobs {
171 my $blobService = shift;
172 if ( wantarray ) {
173 my @blobs = $blobService->list( @_ );
174 return @blobs;
175 }
176 my $blobs = $blobService->list( @_ );
177 return $blobs;
178 }
179  
180 sub put_blob {
181 my $blobService = shift;
182 return $blobService->_put( @_ );
183 }
184  
185 sub get_blob {
186 my $blobService = shift;
187 return $blobService->_get( @_ );
188 }
189  
190 sub get_blob_properties {
191 my $blobService = shift;
192 return $blobService->get_properties( @_ );
193 }
194  
195 sub set_blob_properties {
196 my $blobService = shift;
197 return $blobService->set_properties( @_ );
198 }
199  
200 sub get_blob_metadata {
201 my $blobService = shift;
202 return $blobService->get_metadata( @_ );
203 }
204  
205 sub set_blob_metadata {
206 my $blobService = shift;
207 return $blobService->set_metadata( @_ );
208 }
209  
210 sub snapshot_blob {
211 my $blobService = shift;
212 my ( $path, $params ) = @_;
213 $path = $blobService->_adjust_path( $path );
214 my $data = 'comp=snapshot';
215 my $options = $params->{ options };
216 $data .= '&' . $options if $options;
217 $path = "${path}?${data}";
218 $params->{ body } = $data;
219 return $blobService->put( $path, $params );
220 }
221  
222 sub copy_blob {
223 my $blobService = shift;
224 my ( $src, $path, $params ) = @_;
225 $path = $blobService->_adjust_path( $path );
226 $src = $blobService->_adjust_path( $src );
227 my $data = '';
228 my $account = $blobService->{ account_name };
229 my $protocol = $blobService->{ protocol };
230 my $type = lc( $blobService->{ type } );
231 my $options = $params->{ options };
232 $path .= '?' . $options if $options;
233 $data .= '?' . $options if $options;
234 my $url = "${protocol}://${account}.${type}.core.windows.net/${src}";
235 $params->{ headers }->{ 'x-ms-copy-source' } = $url;
236 $params->{ body } = $data;
237 return $blobService->put( $path, $params );
238 }
239  
240 sub abort_copy_blob {
241 my $blobService = shift;
242 my ( $path, $params ) = @_;
243 $path = $blobService->_adjust_path( $path );
244 my $data = 'comp=copy&copyid=' . $params->{ copyid };
245 my $options = $params->{ options };
246 $data .= '&' . $options if $options;
247 $path = "${path}?${data}";
248 $params->{ body } = $data;
249 return $blobService->put( $path, $params );
250 }
251  
252 sub delete_blob {
253 my $blobService = shift;
254 return $blobService->remove( @_ );
255 }
256  
257 sub lease_blob {
258 my $blobService = shift;
259 return $blobService->lease( @_ );
260 }
261  
262 sub put_block {
263 my $blobService = shift;
264 my ( $path, $params ) = @_;
265 $path = $blobService->_adjust_path( $path );
266 my $data = 'comp=block&blockid=id' . $params->{ blockid };
267 my $options = $params->{ options };
268 $data .= '&' . $options if $options;
269 $path = "${path}?${data}";
270 $params->{ body } = $data;
271 return $blobService->put( $path, $params );
272 }
273  
274 sub put_block_list {
275 my $blobService = shift;
276 my ( $path, $params ) = @_;
277 $path = $blobService->_adjust_path( $path );
278 my $data = 'comp=blocklist';
279 my $options = $params->{ options };
280 $data .= '&' . $options if $options;
281 $path = "${path}?${data}";
282 my $BlockList = $params->{ BlockList };
283 my $xml = XML::Simple->new;
284 my $body = $xml->XMLout( $BlockList, NoAttr => 1, RootName => 'BlockList' );
285 $body = '<?xml version="1.0" encoding="utf-8"?>' . "\n${body}";
286 $params->{ body } = $body;
287 return $blobService->put( $path, $params );
288 }
289  
290 sub get_block_list {
291 my $blobService = shift;
292 my ( $path, $params ) = @_;
293 $path = $blobService->_adjust_path( $path );
294 my $data = 'comp=blocklist';
295 my $options = $params->{ options };
296 $data .= '&' . $options if $options;
297 $path = "${path}?${data}";
298 return $blobService->get( $path, $params );
299 }
300  
301 sub put_page {
302 my $blobService = shift;
303 my ( $path, $params ) = @_;
304 $path = $blobService->_adjust_path( $path );
305 my $data = 'comp=page';
306 my $options = $params->{ options };
307 $data .= '&' . $options if $options;
308 $path = "${path}?${data}";
309 my $page_write = $params->{ 'page-write' };
310 my $range = $params->{ 'range' };
311 $params->{ headers }->{ 'x-ms-page-write' } = $page_write if $page_write;
312 $params->{ headers }->{ 'x-ms-range' } = $range if $range;
313 $params->{ body } = $data;
314 return $blobService->put( $path, $params );
315 }
316  
317 sub get_page_ranges {
318 my $blobService = shift;
319 my ( $path, $params ) = @_;
320 $path = $blobService->_adjust_path( $path );
321 my $data = 'comp=pagelist';
322 my $options = $params->{ options };
323 $data .= '&' . $options if $options;
324 $path = "${path}?${data}";
325 return $blobService->get( $path, $params );
326 }
327  
328 sub rename_blob {
329 my $blobService = shift;
330 my ( $src, $path, $params ) = @_;
331 my $res = $blobService->copy_blob( $src, $path, $params );
332 $blobService->remove( $src );
333 return $res;
334 }
335  
336 sub download_container {
337 my $blobService = shift;
338 my ( $path, $dirname, $params ) = @_;
339 if ( $path !~ m!/$! ) {
340 $path .= '/';
341 }
342 $params->{ directory } = 1;
343 return $blobService->download( $path, $dirname, $params );
344 }
345  
346 sub download_blob {
347 my $blobService = shift;
348 return $blobService->download( @_ );
349 }
350  
351 sub download {
352 my $blobService = shift;
353 my ( $path, $filename, $params ) = @_;
354 my $dir_info = '';
355 if ( $params->{ directory } || $path =~ m!/$! ) {
356 $dir_info = $blobService->_get_directory_info( $path, $filename, $params );
357 }
358 if ( $dir_info ) {
359 # Download blobs of directory
360 my $excludes = $params->{ excludes } || $params->{ exclude };
361 my $container_name = $dir_info->{ container_name };
362 my $path = $dir_info->{ path };
363 my $blobs = $dir_info->{ blobs };
364 my $files = $dir_info->{ files };
365 my $download_items;
366 my @removed_items;
367 my @removed;
368 my $prefix = quotemeta( $path );
369 my $base = quotemeta( $filename );
370 require HTTP::Date;
371 my @_blobs;
372 for my $blob ( @$blobs ) {
373 my $name = $blob->{ Name };
374 if ( my $meta = $blob->{ Metadata } ) {
375 if ( my $original = $meta->{ Path } ) {
376 $name = $original;
377 }
378 }
379 Encode::_utf8_off( $name );
380 next if $prefix && ( $name !~ /^$prefix/ );
381 if ( $excludes ) {
382 my $exclusion;
383 for my $check ( @$excludes ) {
384 my $search = quotemeta( $check );
385 if ( $name =~ m/$search/ ) {
386 $exclusion = 1;
387 last;
388 }
389 }
390 next if $exclusion;
391 }
392 my $real_name = $name;
393 $real_name =~ s/^$prefix// if $prefix;
394 next if _basename( $real_name, '/' ) eq '$$$.$$$';
395 push ( @_blobs, $real_name );
396 my $not_modified;
397 my $rel_path = $name;
398 $rel_path =~ s/^$path// if $path;
399 my $file = File::Spec->catfile( $filename, $rel_path );
400 $file =~ s!/!\\!g if ( $^O eq 'MSWin32' );
401 my $q = quotemeta( $file );
402 if ( grep( /^$q$/, @$files ) ) {
403 if ( $params->{ conditional } || $params->{ sync } ) {
404 if ( -f $file ) {
405 my $etag;
406 if ( my $meta = $blob->{ Metadata } ) {
407 $etag = $meta->{ Etag };
408 }
409 if ( $etag ) {
410 my $data = '';
411 open my $fh, "<$file" or die "Can't open '$file'.";
412 binmode $fh;
413 while ( read $fh, my ( $chunk ), 8192 ) {
414 $data .= $chunk;
415 }
416 close $fh;
417 my $comp = Digest::MD5::md5_hex( $data );
418 if ( $comp eq $etag ) {
419 $not_modified = 1;
420 }
421 } else {
422 my $mtime = $blobService->_get_mtime( $blob );
423 my @stats = stat $file;
424 if ( $stats[ 9 ] >= $mtime ) {
425 $not_modified = 1;
426 }
427 }
428 }
429 }
430 }
431 $download_items->{ $blob->{ Name } } = $file unless $not_modified;
432 }
433 for my $item ( @$files ) {
434 my $rel_path = $item;
435 $rel_path =~ s!\\!/!g if ( $^O eq 'MSWin32' );
436 $rel_path =~ s/^$base//;
437 $rel_path = quotemeta( $rel_path );
438 if (! grep( /^$rel_path$/, @_blobs ) ) {
439 push ( @removed_items, $item );
440 }
441 }
442 my @responses;
443 if ( my $thread = $params->{ use_thread } ) {
444 require Net::Azure::StorageClient::Blob::Thread;
445 @responses = Net::Azure::StorageClient::Blob::Thread::download_use_thread(
446 $blobService,
447 { download_items => $download_items,
448 params => $params,
449 container_name => $container_name,
450 thread => $thread } );
451 } else {
452 for my $key ( keys %$download_items ) {
453 $params->{ force } = 1;
454 my $item;
455 if ( $blobService->{ container_name } ) {
456 $item = $key;
457 } else {
458 $item = $container_name . '/' . $key;
459 }
460 $params->{ directory } = undef;
461 my $res = $blobService->download( $item,
462 $download_items->{ $key },
463 $params );
464 push ( @responses, $res );
465 }
466 }
467 if ( $params->{ sync } ) {
468 my $not_remove = $params->{ not_remove };
469 for my $remove( @removed_items ) {
470 if ( $not_remove ) {
471 my $exclusion;
472 for my $check ( @$not_remove ) {
473 my $search = quotemeta( $check );
474 if ( $remove =~ m/$search/ ) {
475 $exclusion = 1;
476 last;
477 }
478 }
479 next if $exclusion;
480 }
481 if ( unlink $remove ) {
482 push ( @removed, $remove );
483 }
484 }
485 }
486 if ( $params->{ sync } ) {
487 my $response = { responses => \@responses,
488 removed_files => \@removed };
489 return $response;
490 }
491 return \@responses if @responses;
492 return undef;
493 }
494 $params->{ filename } = $filename;
495 return $blobService->_get( $path, $params );
496 }
497  
498 sub upload_container {
499 my $blobService = shift;
500 my ( $path, $dirname, $params ) = @_;
501 if ( $path !~ m!/$! ) {
502 $path .= '/';
503 }
504 return $blobService->upload( $path, $dirname, $params );
505 }
506  
507 sub upload_blob {
508 my $blobService = shift;
509 return $blobService->upload( @_ );
510 }
511  
512 sub upload {
513 my $blobService = shift;
514 my ( $path, $filename, $params ) = @_;
515 my $dir_info = '';
516 if ( $params->{ directory } || $path =~ m!/$! ) {
517 $dir_info = $blobService->_get_directory_info( $path, $filename, $params );
518 }
519 if ( $dir_info ) {
520 # Upload files of directory
521 my $excludes = $params->{ excludes } || $params->{ exclude };
522 my $container_name = $dir_info->{ container_name };
523 my $path = $dir_info->{ path };
524 my $blobs = $dir_info->{ blobs };
525 my $files = $dir_info->{ files };
526 my @upload_items;
527 my @not_modified_items;
528 my @removed_items;
529 my $prefix = quotemeta( $path );
530 my $search_dir = quotemeta( $filename );
531 if ( $params->{ conditional } || $params->{ sync } ) {
532 for my $blob ( @$blobs ) {
533 my $name = $blob->{ Name };
534 if ( my $meta = $blob->{ Metadata } ) {
535 if ( my $original = $meta->{ Path } ) {
536 $name = $original;
537 }
538 }
539 next if ( _basename( $name, '/' ) eq '$$$.$$$' );
540 next if $prefix && ( $name !~ /^$prefix/ );
541 my $real_name = $name;
542 $real_name =~ s/$prefix// if $prefix;
543 my $file = File::Spec->catfile( $filename, $real_name );
544 $file =~ s!/!\\!g if ( $^O eq 'MSWin32' );
545 my $q = quotemeta( $file );
546 if ( grep( /^$q$/, @$files ) || ( -f $file ) ) {
547 if ( -f $file ) {
548 my $etag;
549 if ( my $meta = $blob->{ Metadata } ) {
550 $etag = $meta->{ Etag };
551 }
552 if ( $etag ) {
553 my $data = '';
554 open my $fh, "<$file" or die "Can't open '$file'.";
555 binmode $fh;
556 while ( read $fh, my ( $chunk ), 8192 ) {
557 $data .= $chunk;
558 }
559 close $fh;
560 $params->{ contents }->{ $filename } = $data;
561 my $comp = Digest::MD5::md5_hex( $data );
562 if ( $comp eq $etag ) {
563 push ( @not_modified_items, _encode_path( $file ) );
564 }
565 } else {
566 my $mtime = $blobService->_get_mtime( $blob );
567 my @stats = stat $file;
568 push ( @not_modified_items, $file )
569 if ( $stats[ 9 ] <= $mtime );
570 }
571 }
572 } else {
573 push ( @removed_items, $name ) if ( $params->{ sync } );
574 }
575 }
576 for my $item ( @$files ) {
577 my $q = quotemeta( _encode_path( $item ) );
578 if (! grep( /^$q$/, @not_modified_items ) ) {
579 push ( @upload_items, $item );
580 }
581 }
582 } else {
583 @upload_items = @$files;
584 }
585 my @responses;
586 my $uploads;
587 for my $file ( @upload_items ) {
588 if ( $excludes ) {
589 my $exclusion;
590 for my $check ( @$excludes ) {
591 my $search = quotemeta( $check );
592 if ( $file =~ m/$search/ ) {
593 $exclusion = 1;
594 last;
595 }
596 }
597 next if $exclusion;
598 }
599 my $item = $file;
600 $item =~ s/^$search_dir//;
601 if ( $blobService->{ container_name } ) {
602 $item = $path . $item;
603 } else {
604 $item = $container_name . '/' . $path . $item;
605 }
606 if ( $params->{ use_thread } ) {
607 $uploads->{ $item } = $file;
608 } else {
609 $params->{ force } = 1;
610 my $res = $blobService->upload( $item, $file, $params );
611 push ( @responses, $res );
612 }
613 }
614 if ( my $thread = $params->{ use_thread } ) {
615 require Net::Azure::StorageClient::Blob::Thread;
616 @responses = Net::Azure::StorageClient::Blob::Thread::upload_use_thread(
617 $blobService,
618 { upload_items => $uploads,
619 params => $params,
620 thread => $thread } );
621 }
622 if ( $params->{ sync } ) {
623 my $not_remove = $params->{ not_remove };
624 for my $item ( @removed_items ) {
625 if (! $blobService->{ container_name } ) {
626 $item = $container_name . '/' . $item;
627 }
628 if ( $not_remove ) {
629 my $exclusion;
630 for my $check ( @$not_remove ) {
631 my $search = quotemeta( $check );
632 if ( $item =~ m/$search/ ) {
633 $exclusion = 1;
634 last;
635 }
636 }
637 next if $exclusion;
638 }
639 my $res = $blobService->remove( $item, $params );
640 push ( @responses, $res );
641 }
642 }
643 return \@responses if @responses;
644 return undef;
645 }
646 $params->{ filename } = $filename;
647 return $blobService->_put( $path, $params );
648 }
649  
650 sub sync {
651 my $blobService = shift;
652 my ( $path, $directory, $params ) = @_;
653 if ( $path !~ m!/$! ) {
654 $path .= '/';
655 }
656 my $separator = $^O eq 'MSWin32' ? '\\' : '/';
657 if ( $directory !~ m!$separator$! ) {
658 $directory .= $separator;
659 }
660 my $direction = $params->{ direction } || 'upload';
661 $params->{ conditional } = 1;
662 $params->{ sync } = 1;
663 $params->{ directory } = 1;
664 return $blobService->$direction( $path, $directory, $params );
665 }
666  
667 sub list {
668 my $blobService = shift;
669 my ( $path, $params ) = @_;
670 $path = '' unless $path;
671 $path =~ s!^/!!;
672 if ( $path ) {
673 $path .= '?restype=container&comp=list'; # &maxresults=n
674 } else {
675 $path .= '?comp=list';
676 }
677 my $options = $params->{ options };
678 $path .= '&' . $options if $options;
679 my $res = $blobService->get( $path, $params );
680 my @responses;
681 push ( @responses, $res );
682 if ( $res->code != 200 ) {
683 return $res unless wantarray;
684 return \@responses;
685 }
686 my $marker;
687 my $xml = XML::Simple->new;
688 my $data = $res->content;
689 my $list = $xml->XMLin( $data );
690 $marker = $list->{ NextMarker };
691 $marker = undef if ( ( ref $marker ) eq 'HASH' );
692 if (! $marker ) {
693 return $res unless wantarray;
694 } else {
695 while ( $marker ) {
696 $marker =~ s!([^a-zA-Z0-9_.~-])!uc sprintf "%%%02x", ord($1)!eg;
697 my $next = $path . '&marker=' . $marker;
698 my $res = $blobService->get( $next, $params );
699 if ( $res->code != 200 ) {
700 return @responses if wantarray;
701 return \@responses;
702 }
703 push ( @responses, $res );
704 my $xml = XML::Simple->new;
705 my $data = $res->content;
706 my $n_list = $xml->XMLin( $data );
707 $marker = $n_list->{ NextMarker };
708 $marker = undef if ( ( ref $marker ) eq 'HASH' );
709 }
710 }
711 return @responses if wantarray;
712 return \@responses;
713 }
714  
715 sub get_metadata {
716 my $blobService = shift;
717 my ( $path, $params ) = @_;
718 $params->{ 'method' } = 'HEAD';
719 my $options = $params->{ options } || '';
720 $options .= '&' if $options;
721 $options .= 'comp=metadata';
722 $params->{ options } = $options;
723 return $blobService->_get( $path, $params );
724 }
725  
726 sub get_properties {
727 my $blobService = shift;
728 my ( $path, $params ) = @_;
729 $params->{ 'method' } = 'HEAD';
730 return $blobService->_get( $path, $params );
731 }
732  
733 sub set_properties {
734 my $blobService = shift;
735 my ( $path, $params ) = @_;
736 $path = $blobService->_adjust_path( $path );
737 my $options .= 'comp=properties';
738 $options .= '&' . $params->{ options } if $params->{ options };
739 $params->{ options } = $options;
740 my $properties = $params->{ properties };
741 for my $key ( keys %$properties ) {
742 my $property = $key;
743 if ( $key !~ m/^x\-ms\-/ ) {
744 $property = 'x-ms-' . $property;
745 }
746 $params->{ headers }->{ $property } = $properties->{ $property };
747 }
748 $params->{ body } = $options;
749 return $blobService->put( $path, $params );
750 }
751  
752 sub set_metadata {
753 my $blobService = shift;
754 my ( $path, $params ) = @_;
755 $path = $blobService->_adjust_path( $path );
756 my $data = 'comp=metadata';
757 if ( $path !~ m!/! ) {
758 $data = 'restype=container&' . $data;
759 }
760 my $options = $params->{ options };
761 $data .= '&' . $options if $options;
762 $path = "${path}?${data}";
763 my $metadata = $params->{ metadata };
764 for my $key ( keys %$metadata ) {
765 my $meta = $key;
766 if ( $key !~ m/^x\-ms\-meta\-/ ) {
767 $meta = 'x-ms-meta-' . $meta;
768 }
769 $params->{ headers }->{ $meta } = $metadata->{ $key };
770 }
771 $params->{ body } = $data;
772 return $blobService->put( $path, $params );
773 }
774  
775 sub remove {
776 my $blobService = shift;
777 my ( $path, $params ) = @_;
778 $path = $blobService->_adjust_path( $path );
779 if ( $path =~ /\%/ ) {
780 $path = _encode_path( $path, '/' );
781 }
782 if ( $path !~ m!/! ) {
783 return $blobService->delete_container( $path, $params );
784 }
785 my $options = $params->{ options };
786 $path .= '?' . $options if $options;
787 return $blobService->delete( $path, $params );
788 }
789  
790 sub lease {
791 my $blobService = shift;
792 my ( $path, $params ) = @_;
793 $path = $blobService->_adjust_path( $path );
794 my $data = 'comp=lease';
795 if ( $path !~ m!/! ) {
796 $data = 'restype=container&' . $data;
797 }
798 my $options = $params->{ options };
799 $data .= '&' . $options if $options;
800 $path = "${path}?${data}";
801 my $lease_parameters = $params->{ lease_parameters };
802 for my $key ( keys %$lease_parameters ) {
803 my $parameter = $key;
804 if ( $key !~ m/^x\-ms\-/ ) {
805 $parameter = 'x-ms-' . $parameter;
806 }
807 $params->{ headers }->{ $parameter } = $lease_parameters->{ $key };
808 }
809 $params->{ body } = $data;
810 return $blobService->put( $path, $params );
811 }
812  
813 sub _get {
814 my $blobService = shift;
815 my ( $path, $params ) = @_;
816 my $orig_path = $path;
817 $path = $blobService->_adjust_path( $path );
818 my $filename;
819 if ( $params && $params->{ filename } ) {
820 $filename = $params->{ filename };
821 }
822 if ( $filename && ( $params->{ conditional } || $params->{ sync } ) ) {
823 if (! $params->{ force } ) {
824 $params->{ compare } = 'from';
825 my $metadata = $blobService->_do_conditional( $orig_path, $filename, $params );
826 return $metadata if $metadata;
827 }
828 }
829 my $method = $params->{ 'method' };
830 my $separator = '?';
831 if ( ( $path !~ m!/! ) && ( $method && ( $method eq 'HEAD' ) ) ) {
832 $path .= '?restype=container';
833 $separator = '&';
834 }
835 $method = 'GET' unless $method;
836 my $options = $params->{ options };
837 $path .= $separator . $options if $options;
838 $params->{ 'method' } = $method;
839 if ( $path =~ /\%/ ) {
840 $path = _encode_path( $path, '/' );
841 }
842 my $res = $blobService->request( $method, $path, $params );
843 if ( $filename ) {
844 if ( $res->code == 200 ) {
845 my $content = $res->content;
846 my $dir = File::Basename::dirname( $filename );
847 if (! -d $dir ) {
848 require File::Path;
849 File::Path::mkpath( $dir );
850 }
851 if ( -d $dir ) {
852 open my $fh, ">$filename" or die "Can't open '$filename'.";
853 print $fh $content;
854 close $fh ;
855 if ( $params->{ conditional } || $params->{ sync } ) {
856 require HTTP::Date;
857 my $mtime;
858 if( $res->headers->{ 'x-ms-meta-mtime' } ) {
859 $mtime = $res->headers->{ 'x-ms-meta-mtime' };
860 } else {
861 $mtime = $res->headers->{ 'last-modified' };
862 $mtime = HTTP::Date::str2time( $mtime );
863 }
864 if ( -f $filename ) {
865 my @stat = stat $filename;
866 my $atime = $stat[ 8 ];
867 utime $atime, $mtime, $filename;
868 }
869 }
870 }
871 }
872 }
873 return $res;
874 }
875  
876 sub _put {
877 my $blobService = shift;
878 my ( $path, $data, $params ) = @_;
879 my $orig_path = $path;
880 if ( ref $data eq 'HASH' ) {
881 $params = $data;
882 }
883 $path = $blobService->_adjust_path( $path );
884 my $filename = $params->{ filename };
885 my $options = $params->{ options };
886 $path .= '&' . $options if $options;
887 my $blob_type = $params->{ blob_type } || 'BlockBlob';
888 $params->{ headers }->{ 'x-ms-blob-type' } = $blob_type;
889 if (! $params->{ no_metadata } ) {
890 my $mimetype = $params->{ 'content-type' };
891 if (! $mimetype ) {
892 require Net::Azure::StorageClient::MIMEType;
893 $mimetype = Net::Azure::StorageClient::MIMEType::get_mimetype( $path );
894 }
895 $params->{ headers }->{ 'content-type' } = $mimetype;
896 }
897 if ( $filename ) {
898 $data = '';
899 if ( -d $filename ) {
900 $filename = File::Spec->catfile( $filename, _basename( $path, '/' ) );
901 }
902 if ( $params->{ contents } && $params->{ contents }->{ $filename } ) {
903 $data = $params->{ contents }->{ $filename };
904 } else {
905 open my $fh, "<$filename" or die "Can't open '$filename'.";
906 binmode $fh;
907 while ( read $fh, my ( $chunk ), 8192 ) {
908 $data .= $chunk;
909 }
910 close $fh;
911 }
912 if ( $params->{ conditional } || $params->{ sync } ) {
913 if (! $params->{ force } ) {
914 $params->{ compare } = 'to';
915 $params->{ content } = $data;
916 my $metadata = $blobService->_do_conditional( $orig_path, $filename, $params );
917 return $metadata if $metadata;
918 }
919 }
920 if ( -f $filename ) {
921 my @stats = stat $filename;
922 if (! $params->{ no_metadata } ) {
923 $params->{ headers }->{ 'x-ms-meta-mtime' } = $stats[ 9 ];
924 $params->{ headers }->{ 'x-ms-meta-mode' } = sprintf( '%o', $stats[ 2 ] ); # oct()
925 $params->{ headers }->{ 'x-ms-meta-uid' } = $stats[ 4 ];
926 $params->{ headers }->{ 'x-ms-meta-gid' } = $stats[ 5 ];
927 my $etag = Digest::MD5::md5_hex( $data );
928 $params->{ headers }->{ 'x-ms-meta-etag' } = $etag;
929 # Custom header for set timestamp,etag and permission.
930 }
931 }
932 }
933 $params->{ body } = $data;
934 if ( $path =~ /\%/ ) {
935 my $encoded = _encode_path( $path );
936 if ( $encoded ne $path ) {
937 my $name = $path;
938 $name =~ s!^.*?/(.*)$!$1!;
939 $params->{ headers }->{ 'x-ms-meta-path' } = $name;
940 $path = $encoded;
941 }
942 }
943 return $blobService->put( $path, $params );
944 }
945  
946 sub _do_conditional {
947 my ( $blobService, $path, $filename, $params ) = @_;
948 return undef if (! -f $filename );
949 my $metadata = $blobService->get_metadata( $path );
950 if ( $metadata->code == 200 ) {
951 my $conditional;
952 if ( -f $filename ) {
953 my $etag = $metadata->headers->{ 'x-ms-meta-etag' };
954 my $mtime = $metadata->headers->{ 'x-ms-meta-mtime' };
955 my $data = $params->{ content };
956 if ( $etag && (! defined( $data ) ) ) {
957 $data = '';
958 open my $fh, "<$filename" or die "Can't open '$filename'.";
959 binmode $fh;
960 while ( read $fh, my ( $chunk ), 8192 ) {
961 $data .= $chunk;
962 }
963 close $fh;
964 }
965 if ( $etag && defined( $data ) ) {
966 my $comp = Digest::MD5::md5_hex( $data );
967 if ( $comp eq $etag ) {
968 $conditional = 1;
969 }
970 } elsif ( $mtime ) {
971 my @stats = stat $filename;
972 my $compare = $params->{ compare };
973 my $conditional;
974 if ( ( $compare eq 'to' ) && ( $stats[ 9 ] <= $mtime ) ) {
975 $conditional = 1;
976 } elsif ( ( $compare eq 'from' ) && ( $stats[ 9 ] >= $mtime ) ) {
977 $conditional = 1;
978 }
979 }
980 }
981 if ( $conditional ) {
982 $metadata->code( 304 );
983 $metadata->message( 'Not Modified' );
984 return $metadata;
985 }
986 }
987 return undef;
988 }
989  
990 sub _get_mtime {
991 my $blobService = shift;
992 my $blob = shift;
993 my $mtime;
994 if ( my $meta = $blob->{ Metadata } ) {
995 if ( my $blob_mtime = $meta->{ Mtime } ) {
996 $mtime = $blob_mtime;
997 }
998 }
999 if (! $mtime ) {
1000 require HTTP::Date;
1001 $mtime = $blob->{ Properties }->{ 'Last-Modified' };
1002 $mtime = HTTP::Date::str2time( $mtime );
1003 }
1004 return $mtime;
1005 }
1006  
1007 sub _get_directory_info {
1008 my ( $blobService, $path, $dirname, $params ) = @_;
1009 $path = $blobService->_adjust_path( $path );
1010 if ( $path !~ m!/! ) {
1011 $path .= '/';
1012 }
1013 if ( $path =~ m!/$! ) {
1014 # Upload or Download directory
1015 $path = '' unless $path;
1016 $path =~ s!^/!!;
1017 my $container_name = $blobService->{ container_name };
1018 if (! $container_name ) {
1019 my @split_path = split( /\//, $path );
1020 $container_name = $split_path[ 0 ];
1021 $path =~ s!^$container_name/!!;
1022 } else {
1023 $path =~ s/^$container_name//;
1024 $path =~ s!^/!!;
1025 }
1026 return undef unless $container_name;
1027 my $dir = _basename( $path, '/', 'dirname' );
1028 if ( $dir eq '.' ) {
1029 $dir = $path;
1030 } else {
1031 $dir .= '/';
1032 }
1033 my $options = 'include=metadata';
1034 if ( $dir ) {
1035 $options .= '&prefix=' . $dir;
1036 }
1037 my $blobs;
1038 my $list_params = { options => $options };
1039 my $res = $blobService->list( $container_name, $list_params );
1040 my $responses;
1041 if ( ( ref $res ) ne 'ARRAY' ) {
1042 push ( @$responses, $res );
1043 } else {
1044 $responses = $res;
1045 }
1046 for my $res ( @$responses ) {
1047 if ( $res->code != 200 ) {
1048 die $res->message;
1049 }
1050 my $data = $res->content;
1051 my $xml = XML::Simple->new;
1052 my $list = $xml->XMLin( $data );
1053 if ( my $blob_list = $list->{ Blobs }->{ Blob } ) {
1054 if ( ref( $blob_list ) eq 'HASH' ) {
1055 push ( @$blobs, $blob_list );
1056 } else {
1057 push ( @$blobs, @$blob_list );
1058 }
1059 }
1060 }
1061 require File::Find;
1062 my $files;
1063 if ( -d $dirname ) {
1064 my $separator = $^O eq 'MSWin32' ? '\\' : '/';
1065 my $search_base = quotemeta( $dirname . $separator );
1066 my $command = 'File::Find::find( sub {
1067 my $file = $File::Find::name;
1068 $file =~ s/^$search_base//;
1069 my $basename = File::Basename::basename( $_ );
1070 if ( $params->{ include_invisible } ) {
1071 push( @$files, $file ) if ( -f $File::Find::name and $basename !~ /^\.{1,}$/ );
1072 } else {
1073 my @fileparse = File::Spec->splitdir( $file );
1074 # push( @$files, $file ) if ( -f $File::Find::name and $basename !~ /^\./ );
1075 push( @$files, $file ) if ( -f $File::Find::name and (! grep( /^\./, @fileparse ) ) );
1076 } },
1077 $dirname );';
1078 eval $command;
1079 if ( $@ ) {
1080 return die $@;
1081 }
1082 }
1083 my $dir_info = { container_name => $container_name,
1084 path => $path,
1085 blobs => $blobs,
1086 files => $files };
1087 return $dir_info;
1088 }
1089 return undef;
1090 }
1091  
1092 sub _encode_path {
1093 my ( $filename, $separator ) = @_;
1094 Encode::_utf8_off( $filename );
1095 my @fileparse;
1096 if (! $separator ) {
1097 $separator = $^O eq 'MSWin32' ? '\\' : '/';
1098 @fileparse = File::Spec->splitdir( $filename );
1099 } else {
1100 my $q = quotemeta( $separator );
1101 @fileparse = split( /$q/, $filename );
1102 }
1103 my @paths;
1104 for my $path ( @fileparse ) {
1105 $path =~ s!([^a-zA-Z0-9_.~-])!uc sprintf "%%%02x", ord($1)!eg;
1106 push ( @paths, $path );
1107 }
1108 $filename = join( $separator, @paths );
1109 return $filename;
1110 }
1111  
1112 sub _basename {
1113 my ( $filename, $separator, $want ) = @_;
1114 if (! $separator ) {
1115 $separator = $^O eq 'MSWin32' ? '\\' : '/';
1116 }
1117 $want = 'basename' unless $want;
1118 my $basename;
1119 if ( ( ( $^O ne 'MSWin32' ) && ( $separator eq '/' ) ) ||
1120 ( ( $^O eq 'MSWin32' ) && ( $separator eq '\\' ) ) ) {
1121 if ( $want eq 'dirname' ) {
1122 $basename = File::Basename::dirname( $filename );
1123 } elsif ( $want eq 'basename' ) {
1124 $basename = File::Basename::basename( $filename );
1125 }
1126 } else {
1127 my $q = quotemeta( $separator );
1128 $filename =~ s/$q$//;
1129 my @fileparse = split( /$q/, $filename );
1130 if ( $want eq 'dirname' ) {
1131 pop @fileparse;
1132 $basename = join( $separator, @fileparse );
1133 if (! $basename ) {
1134 $basename = '.';
1135 }
1136 } else {
1137 $basename = pop @fileparse;
1138 }
1139 }
1140 return $basename;
1141 }
1142  
1143 1;
1144  
1145 __END__