File Coverage

blib/lib/Data/Save/S3.pm
Criterion Covered Total %
statement 28 51 54.9
branch 0 14 0.0
condition 0 2 0.0
subroutine 10 13 76.9
pod 0 4 0.0
total 38 84 45.2


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2             # -I/home/phil/z/perl/cpan/DataTableText/lib
3             #-------------------------------------------------------------------------------
4             # Zip some files to Amazon S3
5             # Philip R Brenan at gmail dot com, Appa Apps Ltd, 2016
6             #-------------------------------------------------------------------------------
7             # podDocumentation
8              
9             package Data::Save::S3;
10             require v5.16.0;
11 1     1   473 use warnings FATAL => qw(all);
  1         2  
  1         32  
12 1     1   4 use strict;
  1         2  
  1         16  
13 1     1   4 use Carp;
  1         1  
  1         67  
14 1     1   441 use Data::Table::Text qw(:all);
  1         24749  
  1         620  
15             our $VERSION = 20170801;
16              
17             #1 Zip and Send to S3 # Zip the named files into one folder, zip the folder and send it to AWS S3
18              
19             sub new # New zipper
20 0     0 0 0 {bless {}
21             }
22              
23             genLValueArrayMethods (qw(files)); # Array of files to zip and send to S3
24             genLValueScalarMethods(qw(profile)); # Optional aws profile to use on the --profile keyword of the aws s3 command
25             genLValueScalarMethods(qw(s3)); # Bucket/folder on S3 into which to upload the zip file, without the initial s3:// or trailing the zip file name
26             genLValueScalarMethods(qw(zip)); # The short name of the zip file minus the zip extension and path
27              
28             sub send($) # Zip and send files to S3.
29 0     0 0 0 {my ($zip) = @_; # Zipper
30              
31 0 0       0 unless(my $missing = &checkEnv) # Check that the necessary commands are installed
32 0         0 {confess "Ensure that 'zip' and 'aws' commands are installed";
33             }
34              
35 0         0 my $z = $zip->zip; # Short zip name
36              
37 0         0 my $tmp = filePathDir(qw(zip), $z); # Create a folder into which we can make temporary copies of the files to process
38 0         0 makePath($tmp); # Make a path to the zip folder
39              
40 0         0 unlink("zip/$z.zip"); # Unlink any existing zip file
41              
42 0         0 for my $file(@{$zip->files}) # Copy files to temporary folder
  0         0  
43 0         0 {my ($F, $f, $e) = parseFileName($file);
44 0         0 my $source = $file;
45 0 0       0 -e $source or confess "File does not exist:\n$source";
46 0         0 my $target = filePathExt($tmp, $f, $e);
47 0 0       0 copy($source, $target) or confess "Copy failed: $!";
48             }
49              
50 0         0 my $s3 = $zip->s3; # Position on S3
51 0   0     0 my $profile = $zip->profile // ''; # Profile keyword
52 0 0       0 $profile = "--profile $profile" if $profile;
53              
54 0         0 xxx("cd zip && zip -mqrT $z $z"); # Zip temporary files
55 0         0 xxx("aws s3 cp zip/$z.zip s3://$s3/$z.zip $profile"); # Send to AWS
56             }
57              
58             sub checkEnv ## Check environment
59 0 0   0 0 0 {return "zip " if qx(zip 2>&1) !~ m/Usage:/; # Zip is not installed
60 0 0       0 return "aws cli" if qx(aws --version 2>&1) !~ m/aws-cli:/; # aws cli is not installed
61             undef
62 0         0 }
63              
64             # Tests and documentation
65              
66 1 0   1 0 10 sub test{eval join('', ) or die $@} test unless caller; # Test
  1     1   3  
  1     1   57  
  1     1   9  
  1     1   3  
  1     1   31  
  1         387  
  1         2612  
  1         78  
  1         495  
  1         54779  
  1         14  
  1         1464  
  1         3  
  1         558  
  1         86  
67              
68             1;
69              
70             # podDocumentation
71              
72             =pod
73              
74             =encoding utf-8
75              
76             =head1 Name
77              
78             Data::Save::S3 - Zip some files to Amazon S3
79              
80             =head1 Synopsis
81              
82             If you have installed the zip command and L then:
83              
84             use Data::Save::S3;
85              
86             my $z = Data::Save::S3::new;
87             $z->zip = qq(latestCode);
88             $z->add = [filePathExt(currentDirectory, qw(test c)))];
89             $z->s3 = qq(bucket/folder);
90             $z->send;
91              
92             produces:
93              
94             cd zip && zip -mqrT DataSaveS3 DataSaveS3
95             aws s3 cp zip/DataSaveS3.zip s3://AppaAppsSourceVersions/DataSaveS3.zip
96             Completed 1.8 KiB/1.8 KiB (296 Bytes/s) with 1 file(s) remaining
97             upload: zip/DataSaveS3.zip to s3://AppaAppsSourceVersions/DataSaveS3.zip
98              
99             =head1 Description
100              
101             =cut
102              
103             # podDocumentation
104              
105             __DATA__