ruby-s3cmd 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,740 @@
1
+ # = ruby-s3cmd - A gem providing a ruby interface to s3cmd Amazon S3 client.
2
+ #
3
+ # Homepage:: http://github.com/jjuliano/ruby-s3cmd
4
+ # Author:: Joel Bryan Juliano
5
+ # Copyright:: (cc) 2011 Joel Bryan Juliano
6
+ # License:: MIT
7
+ #
8
+
9
+ #
10
+ # class RubyS3CMD::S3Cmd.new( array, str, array)
11
+ #
12
+ class RubyS3Cmd::S3Cmd
13
+
14
+ #
15
+ # Options common for all commands (where it makes sense indeed)
16
+ #
17
+
18
+ #
19
+ # Recursive upload, download or removal. When used with del it can remove all the files in a bucket.
20
+ #
21
+ attr_accessor :recursive
22
+
23
+ #
24
+ # Force overwrite and other dangerous operations. Can be used to remove a non-empty buckets with s3cmd
25
+ # rb --force s3://bkt
26
+ #
27
+ attr_accessor :force
28
+
29
+ #
30
+ # Specify datacentre where to create the bucket. Possible values are US (default) or EU.
31
+ #
32
+ attr_accessor :bucket_location
33
+
34
+ #
35
+ # Print sizes in human readable form.
36
+ #
37
+ attr_accessor :human_readable_sizes
38
+
39
+ #
40
+ # Include MD5 sums in bucket listings (only for ls command).
41
+ #
42
+ attr_accessor :list_md5
43
+
44
+ #
45
+ # Display or don't display progress meter. When running on TTY (e.g. console or xterm) the default is
46
+ # to display progress meter. If not on TTY (e.g. output is redirected somewhere or running from cron)
47
+ # the default is to not display progress meter.
48
+ #
49
+ attr_accessor :progress, :no_progress
50
+
51
+ #
52
+ # Override autodetected terminal and filesystem encoding (character set).
53
+ #
54
+ attr_accessor :encoding
55
+
56
+ #
57
+ # Enable verbose output.
58
+ #
59
+ attr_accessor :verbose
60
+
61
+ #
62
+ # Enable debug output.
63
+ #
64
+ attr_accessor :debug
65
+
66
+ #
67
+ # Config file related options.
68
+ #
69
+
70
+ #
71
+ # Config file name. Defaults to $HOME/.s3cfg
72
+ #
73
+ attr_accessor :config
74
+
75
+ #
76
+ # Options specific for file transfer commands (sync, put and get):
77
+ #
78
+
79
+ #
80
+ # Only show what should be uploaded or downloaded but don't actually do it. May still perform S3
81
+ # requests to get bucket listings and other in formation though.
82
+ #
83
+ attr_accessor :dry_run
84
+
85
+ #
86
+ # Delete remote objects with no corresponding local file when syncing to S3 or delete local files with
87
+ # no corresponding object in S3 when syncing from S3.
88
+ #
89
+ attr_accessor :delete_removed
90
+
91
+ #
92
+ # Don't delete remote objects. Default for sync command.
93
+ #
94
+ attr_accessor :no_delete_removed
95
+
96
+ #
97
+ # Preserve filesystem attributes (mode, ownership, timestamps). Default for sync command.
98
+ #
99
+ attr_accessor :preserve
100
+
101
+ #
102
+ # Don't store filesystem attributes with uploaded files.
103
+ #
104
+ attr_accessor :no_preserve
105
+
106
+ #
107
+ # Exclude files matching GLOB (a.k.a. shell-style wildcard) from sync. See FILE TRANSFERS section and
108
+ # http://s3tools.org/s3cmd-sync for more information.
109
+ #
110
+ attr_accessor :exclude # GLOB
111
+
112
+ #
113
+ # Same as --exclude but reads GLOBs from the given FILE instead of expecting them on the command line.
114
+ #
115
+ attr_accessor :exclude_from # FILE
116
+
117
+ #
118
+ # Same as --exclude but works with REGEXPs (Regular expressions).
119
+ #
120
+ attr_accessor :rexclude # REGEXP
121
+
122
+ #
123
+ # Same as --exclude-from but works with REGEXPs.
124
+ #
125
+ attr_accessor :rexclude_from # FILE
126
+
127
+ #
128
+ # Filenames and paths matching GLOB or REGEXP will be included even if previously excluded by one of
129
+ # --(r)exclude(-from) patterns
130
+ #
131
+ attr_accessor :include # GLOB
132
+ attr_accessor :include_from # FILE
133
+ attr_accessor :rinclude # REGEXP
134
+ attr_accessor :rinclude_from # FILE
135
+
136
+ #
137
+ # Continue getting a partially downloaded file (only for get command). This comes handy once download
138
+ # of a large file, say an ISO image, from a S3 bucket fails and a partially downloaded file is left on
139
+ # the disk. Unfortunately put command doesn't support restarting of failed upload due to Amazon S3
140
+ # limitations.
141
+ #
142
+ attr_accessor :continue
143
+
144
+ #
145
+ # Skip over files that exist at the destination (only for get and sync commands).
146
+ #
147
+ attr_accessor :skip_existing
148
+
149
+ #
150
+ # Default MIME-type to be set for objects stored.
151
+ #
152
+ attr_accessor :mime_type # MIME/TYPE
153
+
154
+ #
155
+ # Guess MIME‐type of files by their extension. Falls back to default MIME‐Type as specified by
156
+ # --mime-type option
157
+ #
158
+ attr_accessor :guess_mime_type
159
+
160
+ #
161
+ # Add a given HTTP header to the upload request. Can be used multiple times with different header
162
+ # names. For instance set 'Expires' or 'Cache-Control' headers (or both) using this options if you
163
+ # like.
164
+ #
165
+ attr_accessor :add_header # NAME:VALUE
166
+
167
+ #
168
+ # Store objects with permissions allowing read for anyone. See http://s3tools.org/s3cmd-public for
169
+ # details and hints for storing publicly accessible files.
170
+ #
171
+ attr_accessor :acl_public
172
+
173
+ #
174
+ # Store objects with default ACL allowing access for you only.
175
+ #
176
+ attr_accessor :acl_private
177
+
178
+ #
179
+ # Use GPG encryption to protect stored objects from unauthorized access. See http://s3tools.org/s3cmd-
180
+ # public for details about encryption.
181
+ #
182
+ attr_accessor :encrypt
183
+
184
+ #
185
+ # Don't encrypt files.
186
+ #
187
+ attr_accessor :no_encrypt
188
+
189
+ #
190
+ # Options for CloudFront commands
191
+ #
192
+ # See http://s3tools.org/s3cmd-cloudfront for more details.
193
+ #
194
+
195
+ #
196
+ # Enable given CloudFront distribution (only for cfmodify command)
197
+ #
198
+ attr_accessor :enable
199
+
200
+ #
201
+ # Enable given CloudFront distribution (only for cfmodify command)
202
+ #
203
+ attr_accessor :disable
204
+
205
+ #
206
+ # Add given CNAME to a CloudFront distribution (only for cfcreate and cfmodify commands)
207
+ #
208
+ attr_accessor :cf_add_cname # CNAME
209
+
210
+ #
211
+ # Remove given CNAME from a CloudFront distribution (only for cfmodify command)
212
+ #
213
+ attr_accessor :cf_remove_cname # CNAME
214
+
215
+ #
216
+ # Set COMMENT for a given CloudFront distribution (only for cfcreate and cfmodify commands)
217
+ #
218
+ attr_accessor :cf_comment # COMMENT
219
+
220
+ #
221
+ # Sets the executable path, otherwise the environment path will be used.
222
+ #
223
+ attr_accessor :path_to_s3cmd
224
+
225
+ #
226
+ # Returns a new RubyS3CMD::S3Cmd Object
227
+ #
228
+ def initialize()
229
+ end
230
+
231
+ #
232
+ # Show the help message and exit
233
+ #
234
+ def help
235
+ tmp = Tempfile.new('tmp')
236
+ command = option_string() + "--help " + " 2> " + tmp.path
237
+ success = system(command)
238
+ if success
239
+ begin
240
+ while (line = tmp.readline)
241
+ line.chomp
242
+ selected_string = line
243
+ end
244
+ rescue EOFError
245
+ tmp.close
246
+ end
247
+ return selected_string
248
+ else
249
+ tmp.close!
250
+ return success
251
+ end
252
+ end
253
+
254
+ #
255
+ # Show s3cmd version and exit.
256
+ #
257
+ def version
258
+ tmp = Tempfile.new('tmp')
259
+ command = option_string() + "--version " + " 2> " + tmp.path
260
+ success = system(command)
261
+ if success
262
+ begin
263
+ while (line = tmp.readline)
264
+ line.chomp
265
+ selected_string = line
266
+ end
267
+ rescue EOFError
268
+ tmp.close
269
+ end
270
+ return selected_string
271
+ else
272
+ tmp.close!
273
+ return success
274
+ end
275
+ end
276
+
277
+ #
278
+ # Invoke interactive (re)configuration tool. Don't worry, you won't lose your settings on subsequent
279
+ # runs.
280
+ #
281
+ def configure
282
+ tmp = Tempfile.new('tmp')
283
+ command = option_string() + "--configure " + " 2> " + tmp.path
284
+ success = system(command)
285
+ if success
286
+ begin
287
+ while (line = tmp.readline)
288
+ line.chomp
289
+ selected_string = line
290
+ end
291
+ rescue EOFError
292
+ tmp.close
293
+ end
294
+ return selected_string
295
+ else
296
+ tmp.close!
297
+ return success
298
+ end
299
+ end
300
+
301
+ #
302
+ # Dump current configuration after parsing config files and command line options and exit.
303
+ #
304
+ def dump_config
305
+ tmp = Tempfile.new('tmp')
306
+ command = option_string() + "--dump-config " + " 2> " + tmp.path
307
+ success = system(command)
308
+ if success
309
+ begin
310
+ while (line = tmp.readline)
311
+ line.chomp
312
+ selected_string = line
313
+ end
314
+ rescue EOFError
315
+ tmp.close
316
+ end
317
+ return selected_string
318
+ else
319
+ tmp.close!
320
+ return success
321
+ end
322
+ end
323
+
324
+ # Make bucket
325
+ def mb(bucket) # s3://BUCKET
326
+ tmp = Tempfile.new('tmp')
327
+ command = option_string() + "mb " + bucket.to_s + " 2> " + tmp.path
328
+ success = system(command)
329
+ if success
330
+ begin
331
+ while (line = tmp.readline)
332
+ line.chomp
333
+ selected_string = line
334
+ end
335
+ rescue EOFError
336
+ tmp.close
337
+ end
338
+ return selected_string
339
+ else
340
+ tmp.close!
341
+ return success
342
+ end
343
+ end
344
+
345
+ # Remove bucket
346
+ def rb(bucket) # s3://BUCKET
347
+ tmp = Tempfile.new('tmp')
348
+ command = option_string() + "rb " + bucket.to_s + " 2> " + tmp.path
349
+ success = system(command)
350
+ if success
351
+ begin
352
+ while (line = tmp.readline)
353
+ line.chomp
354
+ selected_string = line
355
+ end
356
+ rescue EOFError
357
+ tmp.close
358
+ end
359
+ return selected_string
360
+ else
361
+ tmp.close!
362
+ return success
363
+ end
364
+ end
365
+
366
+ # List objects or buckets
367
+ def ls(bucket) # s3://BUCKET[/PREFIX]]
368
+ tmp = Tempfile.new('tmp')
369
+ command = option_string() + "ls " + bucket.to_s + " 2> " + tmp.path
370
+ success = system(command)
371
+ if success
372
+ begin
373
+ while (line = tmp.readline)
374
+ line.chomp
375
+ selected_string = line
376
+ end
377
+ rescue EOFError
378
+ tmp.close
379
+ end
380
+ return selected_string
381
+ else
382
+ tmp.close!
383
+ return success
384
+ end
385
+ end
386
+
387
+ # List all object in all buckets
388
+ def la
389
+ tmp = Tempfile.new('tmp')
390
+ command = option_string() + "la " + " 2> " + tmp.path
391
+ success = system(command)
392
+ if success
393
+ begin
394
+ while (line = tmp.readline)
395
+ line.chomp
396
+ selected_string = line
397
+ end
398
+ rescue EOFError
399
+ tmp.close
400
+ end
401
+ return selected_string
402
+ else
403
+ tmp.close!
404
+ return success
405
+ end
406
+ end
407
+
408
+ # Put file into bucket (i.e. upload to S3)
409
+ def put(files, bucket) # FILE [FILE...] s3://BUCKET[/PREFIX]
410
+ tmp = Tempfile.new('tmp')
411
+ command = option_string() + "put " + files.to_s + " " + bucket.to_s + " 2> " + tmp.path
412
+ success = system(command)
413
+ if success
414
+ begin
415
+ while (line = tmp.readline)
416
+ line.chomp
417
+ selected_string = line
418
+ end
419
+ rescue EOFError
420
+ tmp.close
421
+ end
422
+ return selected_string
423
+ else
424
+ tmp.close!
425
+ return success
426
+ end
427
+ end
428
+
429
+ # Get file from bucket (i.e. download from S3)
430
+ def get(bucket, local_file) # s3://BUCKET/OBJECT LOCAL_FILE
431
+ tmp = Tempfile.new('tmp')
432
+ command = option_string() + "get " + bucket.to_s + " " + local_file.to_s + " 2> " + tmp.path
433
+ success = system(command)
434
+ if success
435
+ begin
436
+ while (line = tmp.readline)
437
+ line.chomp
438
+ selected_string = line
439
+ end
440
+ rescue EOFError
441
+ tmp.close
442
+ end
443
+ return selected_string
444
+ else
445
+ tmp.close!
446
+ return success
447
+ end
448
+ end
449
+
450
+ # Delete file from bucket
451
+ def del(bucket) # s3://BUCKET/OBJECT
452
+ tmp = Tempfile.new('tmp')
453
+ command = option_string() + "del " + bucket.to_s + " 2> " + tmp.path
454
+ success = system(command)
455
+ if success
456
+ begin
457
+ while (line = tmp.readline)
458
+ line.chomp
459
+ selected_string = line
460
+ end
461
+ rescue EOFError
462
+ tmp.close
463
+ end
464
+ return selected_string
465
+ else
466
+ tmp.close!
467
+ return success
468
+ end
469
+ end
470
+
471
+ # Backup a directory tree to S3
472
+ # Restore a tree from S3 to local directory
473
+ def sync(src_object, dest_object) # LOCAL_DIR s3://BUCKET[/PREFIX] or s3://BUCKET[/PREFIX] LOCAL_DIR
474
+ tmp = Tempfile.new('tmp')
475
+ command = option_string() + "sync " + src_object.to_s + " " + dest_object.to_s + " 2> " + tmp.path
476
+ success = system(command)
477
+ if success
478
+ begin
479
+ while (line = tmp.readline)
480
+ line.chomp
481
+ selected_string = line
482
+ end
483
+ rescue EOFError
484
+ tmp.close
485
+ end
486
+ return selected_string
487
+ else
488
+ tmp.close!
489
+ return success
490
+ end
491
+ end
492
+
493
+ # Make a copy of a file (cp) or move a file (mv). Destination can be in the same bucket with a dif‐
494
+ # ferent name or in another bucket with the same or different name. Adding --acl-public will make the
495
+ # destination object publicly accessible (see below).
496
+ def cp(src_bucket, dest_bucket) # s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
497
+ tmp = Tempfile.new('tmp')
498
+ command = option_string() + "cp " + src_bucket.to_s + " " + dest_bucket.to_s + " 2> " + tmp.path
499
+ success = system(command)
500
+ if success
501
+ begin
502
+ while (line = tmp.readline)
503
+ line.chomp
504
+ selected_string = line
505
+ end
506
+ rescue EOFError
507
+ tmp.close
508
+ end
509
+ return selected_string
510
+ else
511
+ tmp.close!
512
+ return success
513
+ end
514
+ end
515
+
516
+ # Make a copy of a file (cp) or move a file (mv). Destination can be in the same bucket with a dif‐
517
+ # ferent name or in another bucket with the same or different name. Adding --acl-public will make the
518
+ # destination object publicly accessible (see below).
519
+ def mv(src_bucket, dest_bucket) # s3://BUCKET1/OBJECT1 s3://BUCKET2[/OBJECT2]
520
+ tmp = Tempfile.new('tmp')
521
+ command = option_string() + "mv " + src_bucket.to_s + " " + dest_bucket.to_s + " 2> " + tmp.path
522
+ success = system(command)
523
+ if success
524
+ begin
525
+ while (line = tmp.readline)
526
+ line.chomp
527
+ selected_string = line
528
+ end
529
+ rescue EOFError
530
+ tmp.close
531
+ end
532
+ return selected_string
533
+ else
534
+ tmp.close!
535
+ return success
536
+ end
537
+ end
538
+
539
+ # Modify Access control list for Bucket or Files. Use with --acl-public or --acl-private
540
+ def setacl(bucket) # s3://BUCKET[/OBJECT]
541
+ tmp = Tempfile.new('tmp')
542
+ command = option_string() + "setacl " + bucket.to_s + " 2> " + tmp.path
543
+ success = system(command)
544
+ if success
545
+ begin
546
+ while (line = tmp.readline)
547
+ line.chomp
548
+ selected_string = line
549
+ end
550
+ rescue EOFError
551
+ tmp.close
552
+ end
553
+ return selected_string
554
+ else
555
+ tmp.close!
556
+ return success
557
+ end
558
+ end
559
+
560
+ # Get various information about a Bucket or Object
561
+ def info(bucket) # s3://BUCKET[/OBJECT]
562
+ tmp = Tempfile.new('tmp')
563
+ command = option_string() + "info " + bucket.to_s + " 2> " + tmp.path
564
+ success = system(command)
565
+ if success
566
+ begin
567
+ while (line = tmp.readline)
568
+ line.chomp
569
+ selected_string = line
570
+ end
571
+ rescue EOFError
572
+ tmp.close
573
+ end
574
+ return selected_string
575
+ else
576
+ tmp.close!
577
+ return success
578
+ end
579
+ end
580
+
581
+ # Disk usage - amount of data stored in S3
582
+ def du(bucket) # [s3://BUCKET[/PREFIX]]
583
+ tmp = Tempfile.new('tmp')
584
+ command = option_string() + "du " + bucket.to_s + " 2> " + tmp.path
585
+ success = system(command)
586
+ if success
587
+ begin
588
+ while (line = tmp.readline)
589
+ line.chomp
590
+ selected_string = line
591
+ end
592
+ rescue EOFError
593
+ tmp.close
594
+ end
595
+ return selected_string
596
+ else
597
+ tmp.close!
598
+ return success
599
+ end
600
+ end
601
+
602
+ # Commands for CloudFront management
603
+
604
+ # List CloudFront distribution points
605
+ def cflist
606
+ tmp = Tempfile.new('tmp')
607
+ command = option_string() + "cflist " + " 2> " + tmp.path
608
+ success = system(command)
609
+ if success
610
+ begin
611
+ while (line = tmp.readline)
612
+ line.chomp
613
+ selected_string = line
614
+ end
615
+ rescue EOFError
616
+ tmp.close
617
+ end
618
+ return selected_string
619
+ else
620
+ tmp.close!
621
+ return success
622
+ end
623
+ end
624
+
625
+ # Display CloudFront distribution point parameters
626
+ def cfinfo(dist_id) # [cf://DIST_ID]
627
+ tmp = Tempfile.new('tmp')
628
+ command = option_string() + "cfinfo " + dist_id.to_s + " 2> " + tmp.path
629
+ success = system(command)
630
+ if success
631
+ begin
632
+ while (line = tmp.readline)
633
+ line.chomp
634
+ selected_string = line
635
+ end
636
+ rescue EOFError
637
+ tmp.close
638
+ end
639
+ return selected_string
640
+ else
641
+ tmp.close!
642
+ return success
643
+ end
644
+ end
645
+
646
+ # Create CloudFront distribution point
647
+ def cfcreate(bucket) # s3://BUCKET
648
+ tmp = Tempfile.new('tmp')
649
+ command = option_string() + "cfcreate " + bucket.to_s + " 2> " + tmp.path
650
+ success = system(command)
651
+ if success
652
+ begin
653
+ while (line = tmp.readline)
654
+ line.chomp
655
+ selected_string = line
656
+ end
657
+ rescue EOFError
658
+ tmp.close
659
+ end
660
+ return selected_string
661
+ else
662
+ tmp.close!
663
+ return success
664
+ end
665
+ end
666
+
667
+ # Delete CloudFront distribution point
668
+ def cfdelete(dist_id) # cf://DIST_ID
669
+ tmp = Tempfile.new('tmp')
670
+ command = option_string() + "cfdelete " + dist_id.to_s + " 2> " + tmp.path
671
+ success = system(command)
672
+ if success
673
+ begin
674
+ while (line = tmp.readline)
675
+ line.chomp
676
+ selected_string = line
677
+ end
678
+ rescue EOFError
679
+ tmp.close
680
+ end
681
+ return selected_string
682
+ else
683
+ tmp.close!
684
+ return success
685
+ end
686
+ end
687
+
688
+ # Change CloudFront distribution point parameters
689
+ def cfmodify(dist_id) # cf://DIST_ID
690
+ tmp = Tempfile.new('tmp')
691
+ command = option_string() + "cfmodify " + dist_id.to_s + " 2> " + tmp.path
692
+ success = system(command)
693
+ if success
694
+ begin
695
+ while (line = tmp.readline)
696
+ line.chomp
697
+ selected_string = line
698
+ end
699
+ rescue EOFError
700
+ tmp.close
701
+ end
702
+ return selected_string
703
+ else
704
+ tmp.close!
705
+ return success
706
+ end
707
+ end
708
+
709
+ def show_config
710
+ option_string()
711
+ end
712
+
713
+ private
714
+
715
+ def option_string()
716
+
717
+ unless @path_to_s3cmd
718
+ ostring = "s3cmd "
719
+ else
720
+ ostring = @path_to_s3cmd + " "
721
+ end
722
+
723
+ self.instance_variables.each do |i|
724
+ tmp_value = self.instance_variable_get "#{i}"
725
+ tmp_string = i.gsub("_", "-").gsub("@", "--")
726
+ unless tmp_string == "--path-to-s3cmd"
727
+ if (tmp_value.is_a? TrueClass) || (tmp_value.is_a? FalseClass)
728
+ ostring += "#{tmp_string} "
729
+ else
730
+ ostring += "#{tmp_string} #{tmp_value} "
731
+ end
732
+ end
733
+ end
734
+
735
+ return ostring
736
+
737
+ end
738
+
739
+ end
740
+