glazed-aws 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +8 -0
- data/LICENSE.txt +202 -0
- data/README.md +3 -0
- data/Rakefile +12 -0
- data/lib/glazed/aws/version.rb +11 -0
- data/lib/glazed/aws.rb +13 -0
- data/lib/glazed/storages/s3.rb +192 -0
- metadata +80 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: abc0ee55acf9c806cea84fd269b250283e24bcdf32f03a41009e3ff75d579b66
|
|
4
|
+
data.tar.gz: 9d468db2b2b4e4264f9c9c0af4a16617513a87f5c3ace4c8714a47bc13d5236b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ae2aa7b00c8ab8be71f2a01b9ad64b05b2c905f8736391b8c291926909cab07e0ed239d48b953a6d568d4c45974509fe1ebf9c957c316a82b14e4d0b5e1b83e9
|
|
7
|
+
data.tar.gz: c4e701032a6baab3fac4a4672be3a4c2d26da14e1d4a1d0153c205184905eb2650b9b7cfb9c7d818ddbeadd99f81039ee6b63947e9300a8eadc2a29e2f422e1e
|
data/.rspec
ADDED
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
## [0.1.0] - 2026-10-14
|
|
2
|
+
|
|
3
|
+
Initial release.
|
|
4
|
+
|
|
5
|
+
### Enhancements
|
|
6
|
+
|
|
7
|
+
- `s3` storage: skips content identical to the stored object by SHA-256 checksum, and writes conditionally (`If-Match`/`If-None-Match`) so concurrent writers are never overwritten unseen
|
|
8
|
+
- `s3` storage supports conditional writes by ETag, so it can hold the `sqlite` state of `glazed orchestrate`
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
data/Rakefile
ADDED
data/lib/glazed/aws.rb
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# typed: strict
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
|
|
6
|
+
require 'base64'
|
|
7
|
+
require 'digest'
|
|
8
|
+
require 'aws-sdk-s3'
|
|
9
|
+
require 'glazed/must'
|
|
10
|
+
require 'glazed/proto'
|
|
11
|
+
require 'glazed/storages/base'
|
|
12
|
+
|
|
13
|
+
module Glazed
|
|
14
|
+
# Namespace of storage implementations; glazed-aws adds {S3}.
|
|
15
|
+
module Storages
|
|
16
|
+
# Amazon S3 bucket. Each file is one object under the prefix; with bucket
|
|
17
|
+
# versioning, pointers name the exact object version written.
|
|
18
|
+
#
|
|
19
|
+
# @example
|
|
20
|
+
# storage = Glazed::Storages::S3.new(bucket: 'netconf', prefix: 'raw/')
|
|
21
|
+
# storage.write(key: 'core/rt1', content: config, message: 'rt1')
|
|
22
|
+
class S3 < Base
|
|
23
|
+
# Content type of stored objects. No charset: devices do not all use UTF-8.
|
|
24
|
+
CONTENT_TYPE = 'text/plain' #: String
|
|
25
|
+
# Attempts at writing an object that other writers keep changing.
|
|
26
|
+
MAX_ATTEMPTS = 3 #: Integer
|
|
27
|
+
|
|
28
|
+
# @param bucket [String] bucket name
|
|
29
|
+
# @param prefix [String] prefix prepended to every key
|
|
30
|
+
# @param region [String, nil] bucket region; the SDK default when nil
|
|
31
|
+
# @param client [Aws::S3::Client, nil] client to use instead of building one
|
|
32
|
+
#: (bucket: String, ?prefix: String, ?region: String?, ?client: ::Aws::S3::Client?) -> void
|
|
33
|
+
def initialize(bucket:, prefix: '', region: nil, client: nil)
|
|
34
|
+
super()
|
|
35
|
+
@bucket = bucket #: String
|
|
36
|
+
@prefix = prefix #: String
|
|
37
|
+
@region = region #: String?
|
|
38
|
+
@client = client #: ::Aws::S3::Client?
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Builds a storage from {Proto::V1::S3Storage}.
|
|
42
|
+
#
|
|
43
|
+
# @param settings [Google::Protobuf::MessageExts] a {Proto::V1::S3Storage}
|
|
44
|
+
# @return [S3] the storage
|
|
45
|
+
# @raise [Must::Violation] when settings is another message
|
|
46
|
+
#: (Google::Protobuf::MessageExts settings) -> S3
|
|
47
|
+
def self.from_proto(settings)
|
|
48
|
+
s3 = Must.be_a(settings, Proto::V1::S3Storage)
|
|
49
|
+
new(bucket: s3.bucket, prefix: s3.prefix, region: s3.region.empty? ? nil : s3.region)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# @return [String] bucket name
|
|
53
|
+
#: String
|
|
54
|
+
attr_reader :bucket
|
|
55
|
+
# @return [String] prefix prepended to every key
|
|
56
|
+
#: String
|
|
57
|
+
attr_reader :prefix
|
|
58
|
+
|
|
59
|
+
# Puts one object per changed file, compared by SHA-256 checksum. Puts are
|
|
60
|
+
# conditional on the object checked, so a concurrent writer is never
|
|
61
|
+
# overwritten unseen. S3 has no multi-object transaction, so the message
|
|
62
|
+
# is not recorded.
|
|
63
|
+
#
|
|
64
|
+
# @param files [Array<Array(String, String)>] keys and contents
|
|
65
|
+
# @param message [Proc] builds a description of the change, unused
|
|
66
|
+
# @return [Array<WriteResult>] the object version holding each file, and whether the file changed
|
|
67
|
+
# @raise [InvalidKey] for an unsafe key
|
|
68
|
+
# @raise [Aws::S3::Errors::ServiceError] when a request fails, or the object keeps changing under concurrent writers
|
|
69
|
+
#: (Array[Base::file] files, message: Base::message_builder) -> Array[WriteResult]
|
|
70
|
+
def write_all(files, message:)
|
|
71
|
+
files.each { |key, _| validate_key(key) }
|
|
72
|
+
files.map { |key, content| write_object("#{prefix}#{key}", content) }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Reads the object version a pointer names.
|
|
76
|
+
#
|
|
77
|
+
# @param pointer [Proto::V1::Pointer] pointer with an s3 location
|
|
78
|
+
# @return [String] the content
|
|
79
|
+
# @raise [ArgumentError] when the pointer is not an s3 location
|
|
80
|
+
# @raise [Aws::S3::Errors::ServiceError] when the object or version does not exist
|
|
81
|
+
#: (Proto::V1::Pointer pointer) -> String
|
|
82
|
+
def read(pointer)
|
|
83
|
+
location = location_of(pointer, Proto::V1::S3Location)
|
|
84
|
+
response = if location.version_id.empty?
|
|
85
|
+
client.get_object(bucket: location.bucket, key: location.key)
|
|
86
|
+
else
|
|
87
|
+
client.get_object(bucket: location.bucket, key: location.key, version_id: location.version_id)
|
|
88
|
+
end
|
|
89
|
+
response.body.read.to_s
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# @return [Boolean] true; versions are ETags
|
|
93
|
+
#: () -> bool
|
|
94
|
+
def conditional? = true
|
|
95
|
+
|
|
96
|
+
# @param key [String] file key
|
|
97
|
+
# @return [String, nil] ETag of the object, or nil when it is missing
|
|
98
|
+
# @raise [InvalidKey] for an unsafe key
|
|
99
|
+
# @raise [Aws::S3::Errors::ServiceError] for errors other than not found
|
|
100
|
+
#: (String key) -> String?
|
|
101
|
+
def version_of(key) = head("#{prefix}#{validate_key(key)}")&.etag
|
|
102
|
+
|
|
103
|
+
# @param key [String] file key
|
|
104
|
+
# @return [Versioned, nil] the content and its ETag, or nil when the object is missing
|
|
105
|
+
# @raise [InvalidKey] for an unsafe key
|
|
106
|
+
# @raise [Aws::S3::Errors::ServiceError] for errors other than not found
|
|
107
|
+
#: (String key) -> Versioned?
|
|
108
|
+
def read_versioned(key)
|
|
109
|
+
response = client.get_object(bucket:, key: "#{prefix}#{validate_key(key)}")
|
|
110
|
+
Versioned.new(content: response.body.read.to_s, version: response.etag)
|
|
111
|
+
rescue ::Aws::S3::Errors::NoSuchKey
|
|
112
|
+
nil
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Puts the object with If-Match on the expected ETag, or If-None-Match
|
|
116
|
+
# when it must not exist yet.
|
|
117
|
+
#
|
|
118
|
+
# @param key [String] file key
|
|
119
|
+
# @param content [String] new content
|
|
120
|
+
# @param expected_version [String, nil] ETag read earlier; nil to create a missing object only
|
|
121
|
+
# @return [String] ETag of the written object
|
|
122
|
+
# @raise [Conflict] when the object holds another version
|
|
123
|
+
# @raise [InvalidKey] for an unsafe key
|
|
124
|
+
# @raise [Aws::S3::Errors::ServiceError] when the request fails otherwise
|
|
125
|
+
#: (key: String, content: String, expected_version: String?) -> String
|
|
126
|
+
def write_if(key:, content:, expected_version:)
|
|
127
|
+
object_key = "#{prefix}#{validate_key(key)}"
|
|
128
|
+
checksum = Base64.strict_encode64(Digest::SHA256.digest(content))
|
|
129
|
+
response = if expected_version
|
|
130
|
+
client.put_object(bucket:, key: object_key, body: content, checksum_sha256: checksum, if_match: expected_version)
|
|
131
|
+
else
|
|
132
|
+
client.put_object(bucket:, key: object_key, body: content, checksum_sha256: checksum, if_none_match: '*')
|
|
133
|
+
end
|
|
134
|
+
response.etag
|
|
135
|
+
rescue ::Aws::S3::Errors::PreconditionFailed, ::Aws::S3::Errors::ConditionalRequestConflict => e
|
|
136
|
+
raise Conflict, "s3://#{bucket}/#{object_key} changed: #{e.message}"
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# @param key [String] object key
|
|
140
|
+
# @param content [String] content
|
|
141
|
+
# @return [WriteResult] the version holding the content, and whether it was written
|
|
142
|
+
# @raise [Aws::S3::Errors::ServiceError] when a request fails or attempts run out
|
|
143
|
+
#: (String key, String content) -> WriteResult
|
|
144
|
+
private def write_object(key, content)
|
|
145
|
+
checksum = Base64.strict_encode64(Digest::SHA256.digest(content))
|
|
146
|
+
attempts = 0
|
|
147
|
+
begin
|
|
148
|
+
attempts += 1
|
|
149
|
+
current = head(key)
|
|
150
|
+
if current && current.checksum_sha256 == checksum
|
|
151
|
+
return WriteResult.new(pointer: pointer(key, current.version_id), changed: false)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
response = if current
|
|
155
|
+
client.put_object(bucket:, key:, body: content, content_type: CONTENT_TYPE, checksum_sha256: checksum, if_match: current.etag)
|
|
156
|
+
else
|
|
157
|
+
client.put_object(bucket:, key:, body: content, content_type: CONTENT_TYPE, checksum_sha256: checksum, if_none_match: '*')
|
|
158
|
+
end
|
|
159
|
+
rescue ::Aws::S3::Errors::PreconditionFailed, ::Aws::S3::Errors::ConditionalRequestConflict
|
|
160
|
+
# Another writer changed the object after it was checked.
|
|
161
|
+
retry if attempts < MAX_ATTEMPTS
|
|
162
|
+
raise
|
|
163
|
+
end
|
|
164
|
+
WriteResult.new(pointer: pointer(key, response.version_id), changed: true)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# @param key [String] object key
|
|
168
|
+
# @return [Aws::S3::Types::HeadObjectOutput, nil] the current object with its checksum, or nil when absent
|
|
169
|
+
# @raise [Aws::S3::Errors::ServiceError] for errors other than not found
|
|
170
|
+
#: (String key) -> ::Aws::S3::Types::HeadObjectOutput?
|
|
171
|
+
private def head(key)
|
|
172
|
+
client.head_object(bucket:, key:, checksum_mode: 'ENABLED')
|
|
173
|
+
rescue ::Aws::S3::Errors::NotFound
|
|
174
|
+
nil
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# @param key [String] object key
|
|
178
|
+
# @param version_id [String, nil] object version; nil for an unversioned bucket
|
|
179
|
+
# @return [Proto::V1::Pointer] pointer to the version
|
|
180
|
+
#: (String key, String? version_id) -> Proto::V1::Pointer
|
|
181
|
+
private def pointer(key, version_id)
|
|
182
|
+
pointer_to(Proto::V1::S3Location.new(bucket:, key:, version_id: version_id.to_s))
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# @return [Aws::S3::Client] the injected client, or one built on first use
|
|
186
|
+
#: () -> ::Aws::S3::Client
|
|
187
|
+
private def client
|
|
188
|
+
@client ||= @region ? ::Aws::S3::Client.new(region: @region) : ::Aws::S3::Client.new
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: glazed-aws
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Sorah Fukumori
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: glazed
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: aws-sdk-s3
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
email:
|
|
41
|
+
- her@sorah.jp
|
|
42
|
+
executables: []
|
|
43
|
+
extensions: []
|
|
44
|
+
extra_rdoc_files: []
|
|
45
|
+
files:
|
|
46
|
+
- ".rspec"
|
|
47
|
+
- ".yardopts"
|
|
48
|
+
- CHANGELOG.md
|
|
49
|
+
- LICENSE.txt
|
|
50
|
+
- README.md
|
|
51
|
+
- Rakefile
|
|
52
|
+
- lib/glazed/aws.rb
|
|
53
|
+
- lib/glazed/aws/version.rb
|
|
54
|
+
- lib/glazed/storages/s3.rb
|
|
55
|
+
homepage: https://github.com/sorah/glazed
|
|
56
|
+
licenses:
|
|
57
|
+
- Apache-2.0
|
|
58
|
+
metadata:
|
|
59
|
+
homepage_uri: https://github.com/sorah/glazed
|
|
60
|
+
source_code_uri: https://github.com/sorah/glazed/tree/main/glazed-aws
|
|
61
|
+
changelog_uri: https://github.com/sorah/glazed/blob/main/glazed-aws/CHANGELOG.md
|
|
62
|
+
rubygems_mfa_required: 'true'
|
|
63
|
+
rdoc_options: []
|
|
64
|
+
require_paths:
|
|
65
|
+
- lib
|
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: 3.4.0
|
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
requirements: []
|
|
77
|
+
rubygems_version: 4.0.20
|
|
78
|
+
specification_version: 4
|
|
79
|
+
summary: AWS integrations for glazed, such as S3 storage
|
|
80
|
+
test_files: []
|