Class: AWS::S3::PresignedPost
- Inherits:
-
Object
- Object
- AWS::S3::PresignedPost
- Includes:
- Core::Model
- Defined in:
- lib/aws/s3/presigned_post.rb
Overview
Helper to generate form fields for presigned POST requests to a bucket. You can use this to create a form that can be used from a web browser to upload objects to S3 while specifying conditions on what can be uploaded and how it is processed and stored.
Defined Under Namespace
Classes: ConditionBuilder
Constant Summary collapse
- SPECIAL_FIELDS =
[:cache_control, :content_type, :content_disposition, :content_encoding, :expires_header, :acl, :server_side_encryption, :success_action_redirect, :success_action_status]
Instance Attribute Summary collapse
-
#bucket ⇒ Bucket
readonly
The bucket to which data can be uploaded using the form fields.
- #conditions ⇒ Object readonly
-
#content_length ⇒ Range
readonly
The range of acceptable object sizes for the upload.
-
#expires ⇒ Object
readonly
The expiration time for the signature.
-
#ignored_fields ⇒ Array<String>
readonly
Additional fields which may be sent with the upload.
-
#key ⇒ String
readonly
The key of the object that will be uploaded.
-
#metadata ⇒ Hash
readonly
A hash of the metadata fields included in the signed fields.
Attributes included from Core::Model
Instance Method Summary collapse
-
#fields ⇒ Hash
A collection of form fields (including a signature and a policy) that can be used to POST data to S3.
-
#initialize(bucket, opts = {}) ⇒ PresignedPost
constructor
Creates a new presigned post object.
-
#policy ⇒ String
The Base64-encoded JSON policy document.
- #refine(opts) ⇒ Object
-
#secure? ⇒ Boolean
True if #url generates an HTTPS url.
-
#url ⇒ URI::HTTP, URI::HTTPS
The URL to which the form fields should be POSTed.
-
#where(field) ⇒ ConditionBuilder
Adds a condition to the policy for the POST.
-
#where_metadata(field) ⇒ ConditionBuilder
Adds a condition to the policy for the POST to constrain the values of metadata fields uploaded with the object.
- #with_equality_condition(option_name, value) ⇒ Object
- #with_prefix_condition(option_name, prefix) ⇒ Object
Methods included from Core::Model
#client, #config_prefix, #inspect
Constructor Details
#initialize(bucket, opts = {}) ⇒ PresignedPost
Creates a new presigned post object.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/aws/s3/presigned_post.rb', line 196 def initialize(bucket, opts = {}) @bucket = bucket @key = opts[:key] @secure = (opts[:secure] != false) @fields = {} SPECIAL_FIELDS.each do |name| @fields[name] = opts[name] if opts.key?(name) end @metadata = opts[:metadata] || {} @content_length = range_value(opts[:content_length]) @conditions = opts[:conditions] || {} @ignored_fields = [opts[:ignore]].flatten.compact @expires = opts[:expires] super @fields[:server_side_encryption] = config.s3_server_side_encryption unless @fields.key?(:server_side_encryption) @fields.delete(:server_side_encryption) if @fields[:server_side_encryption].nil? end |
Instance Attribute Details
#bucket ⇒ Bucket (readonly)
Returns The bucket to which data can be uploaded using the form fields.
58 59 60 |
# File 'lib/aws/s3/presigned_post.rb', line 58 def bucket @bucket end |
#conditions ⇒ Object (readonly)
88 89 90 |
# File 'lib/aws/s3/presigned_post.rb', line 88 def conditions @conditions end |
#content_length ⇒ Range (readonly)
Returns The range of acceptable object sizes for the upload. By default any size object may be uploaded.
74 75 76 |
# File 'lib/aws/s3/presigned_post.rb', line 74 def content_length @content_length end |
#expires ⇒ Object (readonly)
Returns The expiration time for the signature. By default the signature will expire an hour after it is generated.
98 99 100 |
# File 'lib/aws/s3/presigned_post.rb', line 98 def expires @expires end |
#ignored_fields ⇒ Array<String> (readonly)
Returns Additional fields which may be sent with the upload. These will be included in the policy so that they can be sent with any value. S3 will ignore them.
94 95 96 |
# File 'lib/aws/s3/presigned_post.rb', line 94 def ignored_fields @ignored_fields end |
#key ⇒ String (readonly)
Returns The key of the object that will be uploaded. If this is nil, then the object can be uploaded with any key that satisfies the conditions specified for the upload (see #where).
64 65 66 |
# File 'lib/aws/s3/presigned_post.rb', line 64 def key @key end |
#metadata ⇒ Hash (readonly)
Returns A hash of the metadata fields included in the signed fields. Additional metadata fields may be provided with the upload as long as they satisfy the conditions specified for the upload (see #where).
70 71 72 |
# File 'lib/aws/s3/presigned_post.rb', line 70 def @metadata end |
Instance Method Details
#fields ⇒ Hash
Returns A collection of form fields (including a signature and a policy) that can be used to POST data to S3. Additional form fields may be added after the fact as long as they are described by a policy condition (see #where).
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/aws/s3/presigned_post.rb', line 343 def fields signature = config.signer.sign(policy, "sha1") fields = { "AWSAccessKeyId" => config.signer.access_key_id, "key" => key, "policy" => policy, "signature" => signature }.merge(optional_fields) fields["x-amz-security-token"] = config.signer.session_token if config.signer.session_token fields.merge(optional_fields) end |
#policy ⇒ String
Returns The Base64-encoded JSON policy document.
330 331 332 333 334 335 336 |
# File 'lib/aws/s3/presigned_post.rb', line 330 def policy json = { "expiration" => format_expiration, "conditions" => generate_conditions }.to_json Base64.encode64(json).tr("\n","") end |
#refine(opts) ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/aws/s3/presigned_post.rb', line 375 def refine(opts) self.class.new(bucket, { :conditions => conditions, :key => key, :metadata => , :secure => secure?, :content_length => content_length, :expires => expires, :ignore => ignored_fields }.merge(@fields). merge(opts)) end |
#secure? ⇒ Boolean
Returns True if #url generates an HTTPS url.
220 221 222 |
# File 'lib/aws/s3/presigned_post.rb', line 220 def secure? @secure end |
#url ⇒ URI::HTTP, URI::HTTPS
Returns The URL to which the form fields should be POSTed. If you are using the fields in an HTML form, this is the URL to put in the action
attribute of the form tag.
228 229 230 231 232 233 |
# File 'lib/aws/s3/presigned_post.rb', line 228 def url req = Request.new req.bucket = bucket.name req.host = config.s3_endpoint build_uri(req) end |
#where(field) ⇒ ConditionBuilder
Adds a condition to the policy for the POST. Use #where_metadata to add metadata conditions.
306 307 308 309 310 311 |
# File 'lib/aws/s3/presigned_post.rb', line 306 def where(field) raise ArgumentError.new("unrecognized field name #{field}") unless [:key, :content_length, *SPECIAL_FIELDS].include?(field) or field =~ /^x-amz-meta-/ ConditionBuilder.new(self, field) end |
#where_metadata(field) ⇒ ConditionBuilder
Adds a condition to the policy for the POST to constrain the values of metadata fields uploaded with the object. If a metadata field does not have a condition associated with it and is not specified in the constructor (see #metadata) then S3 will reject it.
325 326 327 |
# File 'lib/aws/s3/presigned_post.rb', line 325 def (field) where("x-amz-meta-#{field}") end |
#with_equality_condition(option_name, value) ⇒ Object
362 363 364 365 |
# File 'lib/aws/s3/presigned_post.rb', line 362 def with_equality_condition(option_name, value) field_name = field_name(option_name) with_condition(option_name, Hash[[[field_name, value]]]) end |
#with_prefix_condition(option_name, prefix) ⇒ Object
368 369 370 371 372 |
# File 'lib/aws/s3/presigned_post.rb', line 368 def with_prefix_condition(option_name, prefix) field_name = field_name(option_name) with_condition(option_name, ["starts-with", "$#{field_name}", prefix]) end |