s3_streaming_multipart_uploader 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1ba359075a8dcf7ecfb596714f3b50c0effafae2
4
+ data.tar.gz: f6c2aa214d0953902df54534279baf59eb1a7513
5
+ SHA512:
6
+ metadata.gz: 294887cafefb74b01caea504c76a04ed2d35576cf5ebd2f45528e55ccb57c1ed84d1a927b7790d70bff864c8964d4e924481fc31008e7444ba00da259d6b4470
7
+ data.tar.gz: c12892ecc00f6ee94f89e75bb9ef9d4a6337bfd448e325386ddc79d9b8fbc7f35a21bdf6b47dc62451bd12fce17a7da2076bba43f1f028e2070dc613c5360250
@@ -0,0 +1,93 @@
1
+ class S3StreamingMultipartUploader
2
+
3
+ MIN_PART_SIZE = 1024 * 1024 * 5
4
+
5
+ def initialize(s3_client, options = {})
6
+ @min_part_size = options[:min_part_size] || MIN_PART_SIZE
7
+ @s3_client = s3_client
8
+ end
9
+
10
+ def stream(bucket, key, enumerator)
11
+ @uploader = Uploader.new(@s3_client, bucket, key, enumerator, @min_part_size)
12
+ @uploader.stream
13
+ end
14
+
15
+ private
16
+
17
+ class Uploader
18
+ def initialize(s3_client, bucket, key, enumerator, min_part_size)
19
+ @s3_client = s3_client
20
+ @bucket = bucket
21
+ @key = key
22
+ @enumerator = enumerator
23
+ @min_part_size = min_part_size
24
+ end
25
+
26
+ def stream
27
+ upload_id = start.upload_id
28
+ begin
29
+ parts = stream_parts(upload_id)
30
+ complete(upload_id, parts)
31
+ rescue Aws::Errors::ServiceError => e
32
+ p e
33
+ abort(upload_id)
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def read
40
+ buffer = ""
41
+ begin
42
+ while (chunck = @enumerator.next) && buffer << chunck && buffer.size <= @min_part_size
43
+ p "READ #{chunck.size} #{buffer.size}"
44
+ end
45
+ rescue StopIteration
46
+ end
47
+ buffer
48
+ end
49
+
50
+ def options
51
+ {bucket: @bucket, key: @key}
52
+ end
53
+
54
+ def start
55
+ @s3_client.create_multipart_upload(options)
56
+ end
57
+
58
+ def stream_parts(upload_id)
59
+ i = 1
60
+ parts = []
61
+ while (chunck = read).size > 0
62
+ parts << @s3_client.upload_part(
63
+ options.merge(
64
+ body: chunck,
65
+ upload_id: upload_id,
66
+ part_number: i
67
+ )
68
+ )
69
+ i += 1
70
+ end
71
+ parts.each_with_index.map do |part, i|
72
+ {etag: part.etag, part_number: i + 1}
73
+ end
74
+ end
75
+
76
+ def complete(upload_id, request_parts)
77
+ @s3_client.complete_multipart_upload(
78
+ options.merge(
79
+ upload_id: upload_id,
80
+ multipart_upload: {parts: request_parts}
81
+ )
82
+ )
83
+ end
84
+
85
+ def abort(upload_id)
86
+ @s3_client.abort_multipart_upload(
87
+ options.merge(
88
+ upload_id: upload_id
89
+ )
90
+ )
91
+ end
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3_streaming_multipart_uploader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Fonsan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Stream data to S3 via enumerator
14
+ email: fonsan@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/s3_streaming_multipart_uploader.rb
20
+ homepage: http://github.com/Fonsan/s3_streaming_multipart_uploader
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Stream data to S3 via enumerator
44
+ test_files: []