File Coverage

blib/lib/Data/Save/S3.pm
Criterion Covered Total %
statement 30 63 47.6
branch 1 16 6.2
condition 0 6 0.0
subroutine 10 14 71.4
pod 0 5 0.0
total 41 104 39.4


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, 2017
6             #-------------------------------------------------------------------------------
7             # podDocumentation
8              
9             package Data::Save::S3;
10             require v5.16.0;
11 1     1   581 use warnings FATAL => qw(all);
  1         4  
  1         46  
12 1     1   7 use strict;
  1         3  
  1         32  
13 1     1   8 use Carp;
  1         3  
  1         234  
14 1     1   605 use Data::Table::Text qw(:all);
  1         41801  
  1         1385  
15             our $VERSION = 20170808;
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(folder)); # Folder in which to build the zip file - defaults to B
25             genLValueScalarMethods(qw(profile)); # Optional L profile to use
26             genLValueScalarMethods(qw(s3)); # Bucket/folder on L into which to upload the zip file, without the leading s3:// or trailing zip file name
27             genLValueScalarMethods(qw(zip)); # The short name of the zip file minus the zip extension and path
28              
29             sub send($) # Zip and send files to S3
30 0     0 0 0 {my ($zip) = @_; # Zipper
31              
32 0 0       0 unless(my $missing = &checkEnv) # Check that the necessary commands are installed
33 0         0 {confess "Ensure that 'zip' and 'aws' commands are installed";
34             }
35              
36 0   0     0 my $d = $zip->folder // qq(zip); # Folder in which to create zip file
37 0         0 my $z = $zip->zip; # Short zip name
38 0         0 my $Z = filePathExt($d, $z, qw(zip)); # Long zip file name
39              
40 0         0 my $folder = filePathDir($d, $z); # Create a folder into which we can make temporary copies of the files to process
41 0         0 makePath($folder); # Make a path to the zip folder
42              
43 0         0 unlink($Z); # Unlink any existing zip file
44              
45 0         0 for my $file(@{$zip->files}) # Copy files to temporary folder
  0         0  
46 0         0 {my ($F, $f, $e) = parseFileName($file);
47 0         0 my $source = $file;
48 0 0       0 -e $source or confess "File does not exist:\n$source";
49 0         0 my $target = filePathExt($folder, $f, $e);
50 0 0       0 copy($source, $target) or confess "Copy failed: $!";
51             }
52              
53 0         0 my $s3 = $zip->s3; # Position on S3
54 0   0     0 my $profile = $zip->profile // ''; # Profile keyword
55 0 0       0 $profile = "--profile $profile" if $profile;
56              
57 0         0 xxx("cd $d && zip -mqrT $z $z"); # Zip temporary files
58 0         0 xxx("aws s3 cp $d/$z.zip s3://$s3/$z.zip $profile"); # Send to AWS
59             }
60              
61             sub clean($) # Remove local copy
62 0     0 0 0 {my ($zip) = @_; # Zipper
63 0   0     0 my $d = $zip->folder // qq(zip); # Folder in which to create zip file
64 0         0 my $z = $zip->zip; # Short zip name
65 0         0 my $Z = filePathExt($d, $z, qw(zip)); # Long zip file name
66              
67 0         0 my $folder = filePathDir($d, $z); # Create a folder into which we can make temporary copies of the files to process
68 0         0 unlink($Z); # Unlink local zip file
69 0         0 rmdir $d; # Remove zip folder if empty
70             }
71              
72             sub checkEnv #P Check environment
73 0 0   0 0 0 {return "zip " if qx(zip 2>&1) !~ m/Usage:/; # Zip is not installed
74 0 0       0 return "aws cli" if qx(aws --version 2>&1) !~ m/aws-cli:/; # aws cli is not installed
75             undef
76 0         0 }
77              
78             # Tests and documentation
79              
80             sub test
81 1     1 0 92 {my $s = eval "join('', )";
82 1 50       10 $@ and die $@;
83 1     1   11 eval $s;
  1     1   3  
  1     1   53  
  1     1   6  
  1     1   2  
  1         20  
  1         378  
  1         1927  
  1         62  
  1         460  
  1         57675  
  1         12  
  1         1109  
  1         2  
  1         717  
  1         114  
84 0 0         $@ and die $@;
85             }
86             test unless caller;
87              
88             # podDocumentation
89              
90             =pod
91              
92             =encoding utf-8
93              
94             =head1 Name
95              
96             Data::Save::S3 - Zip some files to Amazon S3
97              
98             =head1 Synopsis
99              
100             The specified L are copied into a sub
101             L/L, then moved into a zip file
102             L/LB<.zip> and uploaded to L using
103             L
104             optionally using a specified L.
105              
106             At the end of the process a copy of the files will exist in
107             the local file: L/LB<.zip> and in the L. If you
108             do not want to keep the locally zipped copy call method L to
109             L it and L the
110             containing L if it is empty.
111              
112             =head2 Required software
113              
114             You should install the B command and
115             L
116             before using this module.
117              
118             =head2 Example
119              
120             use Data::Save::S3;
121              
122             my $z = Data::Save::S3::new;
123             $z->zip = qq(DataSaveS3);
124             $z->add = [filePathExt(currentDirectory, qw(test c)))];
125             $z->s3 = qq(bucket/folder);
126             $z->send;
127              
128             produces:
129              
130             cd zip && zip -mqrT DataSaveS3 DataSaveS3
131             aws s3 cp zip/DataSaveS3.zip s3://AppaAppsSourceVersions/DataSaveS3.zip
132             Completed 1.8 KiB/1.8 KiB (296 Bytes/s) with 1 file(s) remaining
133             upload: zip/DataSaveS3.zip to s3://AppaAppsSourceVersions/DataSaveS3.zip
134              
135             =head1 Description
136              
137             =cut
138              
139             1;
140              
141             # podDocumentation
142              
143             __DATA__