/* * Copyright 2018-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.amazonaws.services.s3.model; import com.amazonaws.AmazonWebServiceRequest; import com.amazonaws.http.HttpMethodName; import java.io.File; import java.io.InputStream; import java.io.Serializable; import java.net.URL; /** * Request class to upload a object to S3 using presigned urls. Upload content can be provided through a file or input stream. * * The behavior is similar to using {@link PutObjectRequest}. Depending on whether a file or input stream is being uploaded, * this request has slightly different behavior. *

*

* When uploading a file: *

* *

* Amazon S3 is a distributed system. If Amazon S3 receives multiple write * requests for the same object nearly simultaneously, all of the objects might * be stored. However, only one object will obtain the key. *

* * If custom headers are used in generating the url, you can set them through {@link #putCustomRequestHeader(String, String)} * method. If any signed headers or parameters are missing from the request, a Signature mismatch error will be thrown from S3. * Ensure you set all the custom headers/parameters that are used for creating the presigned url. */ public class PresignedUrlUploadRequest extends AmazonWebServiceRequest implements S3DataSource, Serializable { private static final long serialVersionUID = 1L; private URL presignedUrl; private HttpMethodName httpMethodName = HttpMethodName.PUT; /** * The file containing the data to be uploaded to Amazon S3. You must either * specify a file or an InputStream containing the data to be uploaded to * Amazon S3. */ private File file; /** * The InputStream containing the data to be uploaded to Amazon S3. You must * either specify a file or an InputStream containing the data to be * uploaded to Amazon S3. */ private transient InputStream inputStream; /** * Optional metadata instructing Amazon S3 how to handle the uploaded data * (e.g. custom user metadata, hooks for specifying content type, etc.). If * you are uploading from an InputStream, you should always * specify metadata with the content size set, otherwise the contents of the * InputStream will have to be buffered in memory before they can be sent to * Amazon S3, which can have very negative performance impacts. */ private ObjectMetadata metadata; public PresignedUrlUploadRequest(URL presignedUrl) { this.presignedUrl = presignedUrl; } /** * @return The presigned url to which the request will be sent */ public URL getPresignedUrl() { return presignedUrl; } /** * @param presignedUrl sets the presigned url to send the request to */ public void setPresignedUrl(URL presignedUrl) { this.presignedUrl = presignedUrl; } /** * @param presignedUrl sets the presigned url to send the request to * @return This object for method chaining */ public PresignedUrlUploadRequest withPresignedUrl(URL presignedUrl) { setPresignedUrl(presignedUrl); return this; } /** * @return The HTTP method (PUT, POST) to be used in this request. The default value is PUT. */ public HttpMethodName getHttpMethodName() { return httpMethodName; } /** * @param httpMethodName Sets the HTTP method (PUT, POST) to be used in this request. The default value is PUT. */ public void setHttpMethodName(HttpMethodName httpMethodName) { this.httpMethodName = httpMethodName; } /** * @param httpMethodName Sets the HTTP method (PUT, POST) to be used in this request. The default value is PUT. * @return This object for method chaining */ public PresignedUrlUploadRequest withHttpMethodName(HttpMethodName httpMethodName) { setHttpMethodName(httpMethodName); return this; } /** * @return the file to upload */ @Override public File getFile() { return file; } /** * @param file Sets the file to upload */ @Override public void setFile(File file) { this.file = file; } /** * @param file Sets the file to upload * @return This object for method chaining */ public PresignedUrlUploadRequest withFile(File file) { setFile(file); return this; } /** * @return the data stream used for the upload */ @Override public InputStream getInputStream() { return inputStream; } /** * @param inputStream the data stream to upload */ @Override public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } /** * @param inputStream the data stream to upload * @return This object for method chaining */ public PresignedUrlUploadRequest withInputStream(InputStream inputStream) { setInputStream(inputStream); return this; } /** * Gets the optional metadata instructing Amazon S3 how to handle the * uploaded data (e.g. custom user metadata, hooks for specifying content * type, etc.). *

* If uploading from an input stream, * always specify metadata with the content size set. Otherwise the * contents of the input stream have to be buffered in memory before * being sent to Amazon S3. This can cause very negative performance * impacts. *

* * @return The optional metadata instructing Amazon S3 how to handle the * uploaded data (e.g. custom user metadata, hooks for specifying * content type, etc.). */ public ObjectMetadata getMetadata() { return metadata; } /** * Sets the optional metadata instructing Amazon S3 how to handle the * uploaded data (e.g. custom user metadata, hooks for specifying content * type, etc.). *

* If uploading from an input stream, * always specify metadata with the content size set. Otherwise the * contents of the input stream have to be buffered in memory before * being sent to Amazon S3. This can cause very negative performance * impacts. *

* * @param metadata * The optional metadata instructing Amazon S3 how to handle the * uploaded data (e.g. custom user metadata, hooks for specifying * content type, etc.). */ public void setMetadata(ObjectMetadata metadata) { this.metadata = metadata; } /** * Sets the optional metadata instructing Amazon S3 how to handle the * uploaded data (e.g. custom user metadata, hooks for specifying content * type, etc.). *

* If uploading from an input stream, * always specify metadata with the content size set. Otherwise the * contents of the input stream have to be buffered in memory before * being sent to Amazon S3. This can cause very negative performance * impacts. *

* * @param metadata * The optional metadata instructing Amazon S3 how to handle the * uploaded data (e.g. custom user metadata, hooks for specifying * content type, etc.). * * @return This object for method chaining */ public PresignedUrlUploadRequest withMetadata(ObjectMetadata metadata) { setMetadata(metadata); return this; } }