| 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 | 1 | require v5.16.0; | |||||||||||
| 11 | 1 | ▶ | use warnings FATAL => qw(all); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 1 | ▶ | use Data::Table::Text qw(:all); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 1 | our $VERSION = 20170809; | |||||||||||
| 16 | |||||||||||||
| 17 | #1 Zip and Send to S3 # L<Copy|http://perldoc.perl.org/File/Copy.html/> the named files into one folder, B<zip> the folder and send the zip archive to L<S3|https://console.aws.amazon.com/s3/home/> | ||||||||||||
| 18 | |||||||||||||
| 19 | sub new # New zipper. | ||||||||||||
| 20 | 0 | ▶ | {bless {} | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 21 | } | ||||||||||||
| 22 | |||||||||||||
| 23 | 1 | genLValueArrayMethods (qw(files)); # Array of files to zip and send to L<S3|https://console.aws.amazon.com/s3/home/> | |||||||||||
| 24 | 1 | genLValueScalarMethods(qw(folder)); # Folder in which to build the zip file - defaults to B<zip/> | |||||||||||
| 25 | 1 | genLValueScalarMethods(qw(profile)); # Optional L<aws-cli|http://docs.aws.amazon.com/cli/latest/userguide/awscli-install-bundle.html> profile to use. | |||||||||||
| 26 | 1 | genLValueScalarMethods(qw(s3)); # Bucket/folder on L<S3|https://console.aws.amazon.com/s3/home/> into which to upload the zip file, without the leading s3:// or trailing zip file name. | |||||||||||
| 27 | 1 | 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 L<S3|https://console.aws.amazon.com/s3/home/> | ||||||||||||
| 30 | 0 | ▶ | {my ($zip) = @_; # Zipper | ||||||||||
|
Subroutine
send: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||
| 31 | |||||||||||||
| 32 | 0 | ▶ | unless(my $missing = &checkEnv) # Check that the necessary commands are installed | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 0 | {confess "Ensure that 'zip' and 'aws' commands are installed"; | |||||||||||
| 34 | } | ||||||||||||
| 35 | |||||||||||||
| 36 | 0 | ▶ | my $d = $zip->folder // qq(zip); # Folder in which to create zip file | ||||||||||
|
Condition0%0/2
$zip->folder // "zip"
MC/DC0%0/1
$zip->folder // "zip"
$zip->folder
Truth table0%0/2
A
A= $zip->folder
| |||||||||||||
| 37 | 0 | my $z = $zip->zip; # Short zip name | |||||||||||
| 38 | 0 | my $Z = filePathExt($d, $z, qw(zip)); # Long zip file name | |||||||||||
| 39 | |||||||||||||
| 40 | 0 | my $folder = filePathDir($d, $z); # Create a folder into which we can make temporary copies of the files to process | |||||||||||
| 41 | 0 | makePath($folder); # Make a path to the zip folder | |||||||||||
| 42 | |||||||||||||
| 43 | 0 | unlink($Z); # Unlink any existing zip file | |||||||||||
| 44 | |||||||||||||
| 45 | 0 | for my $file(@{$zip->files}) # Copy files to temporary folder | |||||||||||
| 46 | 0 | {my ($F, $f, $e) = parseFileName($file); | |||||||||||
| 47 | 0 | my $source = $file; | |||||||||||
| 48 | 0 | ▶ | -e $source or confess "File does not exist:\n$source"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 0 | my $target = filePathExt($folder, $f, $e); | |||||||||||
| 50 | 0 | ▶ | copy($source, $target) or confess "Copy failed: $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | 0 | my $s3 = $zip->s3; # Position on S3 | |||||||||||
| 54 | 0 | ▶ | my $profile = $zip->profile // ''; # Profile keyword | ||||||||||
|
Condition0%0/2
$zip->profile // ""
MC/DC0%0/1
$zip->profile // ""
$zip->profile
Truth table0%0/2
A
A= $zip->profile
| |||||||||||||
| 55 | 0 | ▶ | $profile = "--profile $profile" if $profile; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | |||||||||||||
| 57 | 0 | xxx("cd $d && zip -mqrT $z $z"); # Zip temporary files | |||||||||||
| 58 | 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 | ▶ | {my ($zip) = @_; # Zipper | ||||||||||
|
Subroutine
clean: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 63 | 0 | ▶ | my $d = $zip->folder // qq(zip); # Folder in which to create zip file | ||||||||||
|
Condition0%0/2
$zip->folder // "zip"
MC/DC0%0/1
$zip->folder // "zip"
$zip->folder
Truth table0%0/2
A
A= $zip->folder
| |||||||||||||
| 64 | 0 | my $z = $zip->zip; # Short zip name | |||||||||||
| 65 | 0 | my $Z = filePathExt($d, $z, qw(zip)); # Long zip file name | |||||||||||
| 66 | |||||||||||||
| 67 | 0 | my $folder = filePathDir($d, $z); # Create a folder into which we can make temporary copies of the files to process | |||||||||||
| 68 | 0 | unlink($Z); # Unlink local zip file | |||||||||||
| 69 | 0 | rmdir $d; # Remove zip folder if empty | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | sub checkEnv #P Check environment. | ||||||||||||
| 73 | 0 | ▶ | {return "zip " if qx(zip 2>&1) !~ m/Usage:/; # Zip is not installed | ||||||||||
|
Subroutine
checkEnv: not called CC=3 SCAR=24.8
Pod
documented
Branch: 1 branch
| |||||||||||||
| 74 | 0 | ▶ | return "aws cli" if qx(aws --version 2>&1) !~ m/aws-cli:/; # aws cli is not installed | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | undef | ||||||||||||
| 76 | 0 | } | |||||||||||
| 77 | |||||||||||||
| 78 | # podDocumentation | ||||||||||||
| 79 | |||||||||||||
| 80 | =pod | ||||||||||||
| 81 | |||||||||||||
| 82 | =encoding utf-8 | ||||||||||||
| 83 | |||||||||||||
| 84 | =head1 Name | ||||||||||||
| 85 | |||||||||||||
| 86 | Data::Save::S3 - Zip some files to L<S3|https://console.aws.amazon.com/s3/home/> | ||||||||||||
| 87 | |||||||||||||
| 88 | =head1 Synopsis | ||||||||||||
| 89 | |||||||||||||
| 90 | The specified L<files|/files> are L<copied|http://perldoc.perl.org/File/Copy.html> into a sub | ||||||||||||
| 91 | L<folder|/folder>/L<zip|/zip>, then moved into a zip file | ||||||||||||
| 92 | L<folder|/folder>/L<zip|/zip>B<.zip> and uploaded to L<S3|/s3> using | ||||||||||||
| 93 | L<aws-cli|http://docs.aws.amazon.com/cli/latest/userguide/awscli-install-bundle.html> | ||||||||||||
| 94 | optionally using a specified L<profile|/profile>. | ||||||||||||
| 95 | |||||||||||||
| 96 | At the end of the process a zipped copy of the files will exist in the local | ||||||||||||
| 97 | file: L<folder|/folder>/L<zip|/zip>B<.zip> and in the L<S3 bucket|/s3>. If you | ||||||||||||
| 98 | do not want to keep the locally zipped copy call method L<clean|/clean> to | ||||||||||||
| 99 | L<unlink|http://perldoc.perl.org/functions/unlink.html/> it and | ||||||||||||
| 100 | L<remove|http://perldoc.perl.org/functions/rmdir.html> the containing | ||||||||||||
| 101 | L<folder|/folder> if it is empty. | ||||||||||||
| 102 | |||||||||||||
| 103 | =head2 Required software | ||||||||||||
| 104 | |||||||||||||
| 105 | You should install the B<zip> command and | ||||||||||||
| 106 | L<aws-cli|http://docs.aws.amazon.com/cli/latest/userguide/awscli-install-bundle.html> | ||||||||||||
| 107 | before using this module. | ||||||||||||
| 108 | |||||||||||||
| 109 | =head2 Example | ||||||||||||
| 110 | |||||||||||||
| 111 | use Data::Save::S3; | ||||||||||||
| 112 | |||||||||||||
| 113 | my $z = Data::Save::S3::new; | ||||||||||||
| 114 | $z->zip = qq(DataSaveS3); | ||||||||||||
| 115 | $z->add = [filePathExt(currentDirectory, qw(test c)))]; | ||||||||||||
| 116 | $z->s3 = qq(AppaAppsSourceVersions); | ||||||||||||
| 117 | $z->send; | ||||||||||||
| 118 | |||||||||||||
| 119 | produces: | ||||||||||||
| 120 | |||||||||||||
| 121 | cd zip && zip -mqrT DataSaveS3 DataSaveS3 | ||||||||||||
| 122 | aws s3 cp zip/DataSaveS3.zip s3://AppaAppsSourceVersions/DataSaveS3.zip | ||||||||||||
| 123 | Completed 1.8 KiB/1.8 KiB (296 Bytes/s) with 1 file(s) remaining | ||||||||||||
| 124 | upload: zip/DataSaveS3.zip to s3://AppaAppsSourceVersions/DataSaveS3.zip | ||||||||||||
| 125 | |||||||||||||
| 126 | =head1 Description | ||||||||||||
| 127 | |||||||||||||
| 128 | =head2 Zip and Send to S3 | ||||||||||||
| 129 | |||||||||||||
| 130 | L<Copy|http://perldoc.perl.org/File/Copy.html/> the named files into one folder, B<zip> the folder and send the zip archive to L<S3|https://console.aws.amazon.com/s3/home/> | ||||||||||||
| 131 | |||||||||||||
| 132 | =head3 new | ||||||||||||
| 133 | |||||||||||||
| 134 | New zipper. | ||||||||||||
| 135 | |||||||||||||
| 136 | |||||||||||||
| 137 | =head3 files :lvalue | ||||||||||||
| 138 | |||||||||||||
| 139 | Array of files to zip and send to L<S3|https://console.aws.amazon.com/s3/home/> | ||||||||||||
| 140 | |||||||||||||
| 141 | |||||||||||||
| 142 | =head3 folder :lvalue | ||||||||||||
| 143 | |||||||||||||
| 144 | Folder in which to build the zip file - defaults to B<zip/> | ||||||||||||
| 145 | |||||||||||||
| 146 | |||||||||||||
| 147 | =head3 profile :lvalue | ||||||||||||
| 148 | |||||||||||||
| 149 | Optional L<aws-cli|http://docs.aws.amazon.com/cli/latest/userguide/awscli-install-bundle.html> profile to use. | ||||||||||||
| 150 | |||||||||||||
| 151 | |||||||||||||
| 152 | =head3 s3 :lvalue | ||||||||||||
| 153 | |||||||||||||
| 154 | Bucket/folder on L<S3|https://console.aws.amazon.com/s3/home/> into which to upload the zip file, without the leading s3:// or trailing zip file name. | ||||||||||||
| 155 | |||||||||||||
| 156 | |||||||||||||
| 157 | =head3 zip :lvalue | ||||||||||||
| 158 | |||||||||||||
| 159 | The short name of the zip file minus the zip extension and path. | ||||||||||||
| 160 | |||||||||||||
| 161 | |||||||||||||
| 162 | =head3 send | ||||||||||||
| 163 | |||||||||||||
| 164 | Zip and send files to L<S3|https://console.aws.amazon.com/s3/home/> | ||||||||||||
| 165 | |||||||||||||
| 166 | 1 $zip Zipper | ||||||||||||
| 167 | |||||||||||||
| 168 | =head3 clean | ||||||||||||
| 169 | |||||||||||||
| 170 | Remove local copy. | ||||||||||||
| 171 | |||||||||||||
| 172 | 1 $zip Zipper | ||||||||||||
| 173 | |||||||||||||
| 174 | =head3 checkEnv | ||||||||||||
| 175 | |||||||||||||
| 176 | Check environment. | ||||||||||||
| 177 | |||||||||||||
| 178 | |||||||||||||
| 179 | This is a private method. | ||||||||||||
| 180 | |||||||||||||
| 181 | |||||||||||||
| 182 | |||||||||||||
| 183 | =head1 Index | ||||||||||||
| 184 | |||||||||||||
| 185 | |||||||||||||
| 186 | L<checkEnv|/checkEnv> | ||||||||||||
| 187 | |||||||||||||
| 188 | L<clean|/clean> | ||||||||||||
| 189 | |||||||||||||
| 190 | L<files|/files> | ||||||||||||
| 191 | |||||||||||||
| 192 | L<folder|/folder> | ||||||||||||
| 193 | |||||||||||||
| 194 | L<new|/new> | ||||||||||||
| 195 | |||||||||||||
| 196 | L<profile|/profile> | ||||||||||||
| 197 | |||||||||||||
| 198 | L<s3|/s3> | ||||||||||||
| 199 | |||||||||||||
| 200 | L<send|/send> | ||||||||||||
| 201 | |||||||||||||
| 202 | L<zip|/zip> | ||||||||||||
| 203 | |||||||||||||
| 204 | =head1 Installation | ||||||||||||
| 205 | |||||||||||||
| 206 | This module is written in 100% Pure Perl and, thus, it is easy to read, use, | ||||||||||||
| 207 | modify and install. | ||||||||||||
| 208 | |||||||||||||
| 209 | Standard Module::Build process for building and installing modules: | ||||||||||||
| 210 | |||||||||||||
| 211 | perl Build.PL | ||||||||||||
| 212 | ./Build | ||||||||||||
| 213 | ./Build test | ||||||||||||
| 214 | ./Build install | ||||||||||||
| 215 | |||||||||||||
| 216 | =head1 Author | ||||||||||||
| 217 | |||||||||||||
| 218 | L<philiprbrenan@gmail.com|mailto:philiprbrenan@gmail.com> | ||||||||||||
| 219 | |||||||||||||
| 220 | L<http://www.appaapps.com|http://www.appaapps.com> | ||||||||||||
| 221 | |||||||||||||
| 222 | =head1 Copyright | ||||||||||||
| 223 | |||||||||||||
| 224 | Copyright (c) 2016-2017 Philip R Brenan. | ||||||||||||
| 225 | |||||||||||||
| 226 | This module is free software. It may be used, redistributed and/or modified | ||||||||||||
| 227 | under the same terms as Perl itself. | ||||||||||||
| 228 | |||||||||||||
| 229 | =cut | ||||||||||||
| 230 | |||||||||||||
| 231 | |||||||||||||
| 232 | # Tests and documentation | ||||||||||||
| 233 | |||||||||||||
| 234 | sub test | ||||||||||||
| 235 | 1 | ▶ | {my $p = __PACKAGE__; | ||||||||||
|
Subroutine
test: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||
| 236 | 1 | ▶ | return if eval "eof(${p}::DATA)"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 237 | 1 | my $s = eval "join('', <${p}::DATA>)"; | |||||||||||
| 238 | 1 | ▶ | $@ and die $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 239 | 1 | ▶ | eval $s; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 240 | 0 | ▶ | $@ and die $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 241 | } | ||||||||||||
| 242 | |||||||||||||
| 243 | 1 | ▶ | test unless caller; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 244 | |||||||||||||
| 245 | 1 | 1; | |||||||||||
| 246 | # podDocumentation | ||||||||||||
| 247 | |||||||||||||
| 248 | __DATA__ | ||||||||||||