From 7218b14756345ea58d09c6d5a6da465aa0dacaf8 Mon Sep 17 00:00:00 2001 From: amcofi <11678487+amcofi@users.noreply.github.com> Date: Fri, 28 Jun 2024 14:18:56 +0300 Subject: [PATCH] [aws-storage-s3] Add support to define the S3 Storage Class See also: https://aws.amazon.com/s3/storage-classes --- aws-storage-s3/api/aws-storage-s3.api | 7 +++- .../storage/s3/AWSS3StoragePlugin.java | 13 +++++++ .../AWSS3StoragePathUploadFileOperation.kt | 1 + ...S3StoragePathUploadInputStreamOperation.kt | 2 ++ .../AWSS3StorageUploadFileOperation.kt | 1 + .../AWSS3StorageUploadInputStreamOperation.kt | 1 + .../AWSS3StorageUploadFileOptions.java | 35 +++++++++++++++++++ .../AWSS3StorageUploadInputStreamOptions.java | 30 ++++++++++++++++ .../request/AWSS3StoragePathUploadRequest.kt | 2 ++ .../s3/request/AWSS3StorageUploadRequest.java | 15 ++++++++ 10 files changed, 106 insertions(+), 1 deletion(-) diff --git a/aws-storage-s3/api/aws-storage-s3.api b/aws-storage-s3/api/aws-storage-s3.api index 96aa6229b..ef0aab06d 100644 --- a/aws-storage-s3/api/aws-storage-s3.api +++ b/aws-storage-s3/api/aws-storage-s3.api @@ -242,6 +242,7 @@ public final class com/amplifyframework/storage/s3/options/AWSS3StorageUploadFil public fun equals (Ljava/lang/Object;)Z public static fun from (Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions;)Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions$Builder; public fun getServerSideEncryption ()Lcom/amplifyframework/storage/s3/ServerSideEncryption; + public fun getStorageClass ()Laws/sdk/kotlin/services/s3/model/StorageClass; public fun hashCode ()I public fun toString ()Ljava/lang/String; public fun useAccelerateEndpoint ()Z @@ -253,6 +254,7 @@ public final class com/amplifyframework/storage/s3/options/AWSS3StorageUploadFil public fun build ()Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions; public fun serverSideEncryption (Lcom/amplifyframework/storage/s3/ServerSideEncryption;)Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions$Builder; public fun setUseAccelerateEndpoint (Z)Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions$Builder; + public fun storageClass (Laws/sdk/kotlin/services/s3/model/StorageClass;)Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions$Builder; } public final class com/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions : com/amplifyframework/storage/options/StorageUploadInputStreamOptions { @@ -261,6 +263,7 @@ public final class com/amplifyframework/storage/s3/options/AWSS3StorageUploadInp public fun equals (Ljava/lang/Object;)Z public static fun from (Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions;)Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions$Builder; public fun getServerSideEncryption ()Lcom/amplifyframework/storage/s3/ServerSideEncryption; + public fun getStorageClass ()Laws/sdk/kotlin/services/s3/model/StorageClass; public fun hashCode ()I public fun toString ()Ljava/lang/String; public fun useAccelerateEndpoint ()Z @@ -272,6 +275,7 @@ public final class com/amplifyframework/storage/s3/options/AWSS3StorageUploadInp public fun build ()Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions; public fun serverSideEncryption (Lcom/amplifyframework/storage/s3/ServerSideEncryption;)Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions$Builder; public fun setUseAccelerateEndpoint (Z)Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions$Builder; + public fun storageClass (Laws/sdk/kotlin/services/s3/model/StorageClass;)Lcom/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions$Builder; } public final class com/amplifyframework/storage/s3/request/AWSS3StorageDownloadFileRequest { @@ -312,13 +316,14 @@ public final class com/amplifyframework/storage/s3/request/AWSS3StorageRemoveReq } public final class com/amplifyframework/storage/s3/request/AWSS3StorageUploadRequest { - public fun (Ljava/lang/String;Ljava/lang/Object;Lcom/amplifyframework/storage/StorageAccessLevel;Ljava/lang/String;Ljava/lang/String;Lcom/amplifyframework/storage/s3/ServerSideEncryption;Ljava/util/Map;Z)V + public fun (Ljava/lang/String;Ljava/lang/Object;Lcom/amplifyframework/storage/StorageAccessLevel;Ljava/lang/String;Ljava/lang/String;Lcom/amplifyframework/storage/s3/ServerSideEncryption;Laws/sdk/kotlin/services/s3/model/StorageClass;Ljava/util/Map;Z)V public fun getAccessLevel ()Lcom/amplifyframework/storage/StorageAccessLevel; public fun getContentType ()Ljava/lang/String; public fun getKey ()Ljava/lang/String; public fun getLocal ()Ljava/lang/Object; public fun getMetadata ()Ljava/util/Map; public fun getServerSideEncryption ()Lcom/amplifyframework/storage/s3/ServerSideEncryption; + public fun getStorageClass ()Laws/sdk/kotlin/services/s3/model/StorageClass; public fun getTargetIdentityId ()Ljava/lang/String; public fun useAccelerateEndpoint ()Z } diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/AWSS3StoragePlugin.java b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/AWSS3StoragePlugin.java index e12f047d1..6bce4eb7a 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/AWSS3StoragePlugin.java +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/AWSS3StoragePlugin.java @@ -101,6 +101,7 @@ import java.util.concurrent.TimeUnit; import aws.sdk.kotlin.services.s3.S3Client; +import aws.sdk.kotlin.services.s3.model.StorageClass; /** * A plugin for the storage category which uses S3 as a storage @@ -579,6 +580,9 @@ public StorageUploadFileOperation uploadFile( options instanceof AWSS3StorageUploadFileOptions ? ((AWSS3StorageUploadFileOptions) options).getServerSideEncryption() : ServerSideEncryption.NONE, + options instanceof AWSS3StorageUploadFileOptions + ? ((AWSS3StorageUploadFileOptions) options).getStorageClass() + : StorageClass.Standard.INSTANCE, options.getMetadata(), useAccelerateEndpoint ); @@ -617,6 +621,9 @@ public StorageUploadFileOperation uploadFile( options instanceof AWSS3StorageUploadFileOptions ? ((AWSS3StorageUploadFileOptions) options).getServerSideEncryption() : ServerSideEncryption.NONE, + options instanceof AWSS3StorageUploadFileOptions + ? ((AWSS3StorageUploadFileOptions) options).getStorageClass() + : StorageClass.Standard.INSTANCE, options.getMetadata(), useAccelerateEndpoint ); @@ -707,6 +714,9 @@ public StorageUploadInputStreamOperation uploadInputStream( options instanceof AWSS3StorageUploadInputStreamOptions ? ((AWSS3StorageUploadInputStreamOptions) options).getServerSideEncryption() : ServerSideEncryption.NONE, + options instanceof AWSS3StorageUploadInputStreamOptions + ? ((AWSS3StorageUploadInputStreamOptions) options).getStorageClass() + : StorageClass.Standard.INSTANCE, options.getMetadata(), useAccelerateEndpoint ); @@ -745,6 +755,9 @@ public StorageUploadInputStreamOperation uploadInputStream( options instanceof AWSS3StorageUploadInputStreamOptions ? ((AWSS3StorageUploadInputStreamOptions) options).getServerSideEncryption() : ServerSideEncryption.NONE, + options instanceof AWSS3StorageUploadInputStreamOptions + ? ((AWSS3StorageUploadInputStreamOptions) options).getStorageClass() + : StorageClass.Standard.INSTANCE, options.getMetadata(), useAccelerateEndpoint ); diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathUploadFileOperation.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathUploadFileOperation.kt index 8c5223293..ebd5f1916 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathUploadFileOperation.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathUploadFileOperation.kt @@ -111,6 +111,7 @@ internal class AWSS3StoragePathUploadFileOperation internal constructor( objectMetadata.metaData[ObjectMetadata.SERVER_SIDE_ENCRYPTION] = storageServerSideEncryption.getName() } + objectMetadata.metaData[ObjectMetadata.STORAGE_CLASS] = uploadRequest.storageClass.value transferObserver = storageService.uploadFile( transferId, serviceKey, diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathUploadInputStreamOperation.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathUploadInputStreamOperation.kt index 4b3fd06bd..49d26b41b 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathUploadInputStreamOperation.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StoragePathUploadInputStreamOperation.kt @@ -14,6 +14,7 @@ */ package com.amplifyframework.storage.s3.operation +import aws.sdk.kotlin.services.s3.model.StorageClass import com.amplifyframework.auth.AuthCredentialsProvider import com.amplifyframework.core.Amplify import com.amplifyframework.core.Consumer @@ -110,6 +111,7 @@ internal class AWSS3StoragePathUploadInputStreamOperation internal constructor( objectMetadata.metaData[ObjectMetadata.SERVER_SIDE_ENCRYPTION] = storageServerSideEncryption.getName() } + objectMetadata.metaData[ObjectMetadata.STORAGE_CLASS] = request.storageClass.value transferObserver = storageService.uploadInputStream( transferId, serviceKey, diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StorageUploadFileOperation.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StorageUploadFileOperation.kt index 535b361a9..1b155183d 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StorageUploadFileOperation.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StorageUploadFileOperation.kt @@ -109,6 +109,7 @@ class AWSS3StorageUploadFileOperation @JvmOverloads internal constructor( objectMetadata.metaData[ObjectMetadata.SERVER_SIDE_ENCRYPTION] = storageServerSideEncryption.getName() } + objectMetadata.metaData[ObjectMetadata.STORAGE_CLASS] = uploadRequest.storageClass.value transferObserver = storageService.uploadFile( transferId, serviceKey, diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StorageUploadInputStreamOperation.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StorageUploadInputStreamOperation.kt index 21d463c9a..41e2a527d 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StorageUploadInputStreamOperation.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/operation/AWSS3StorageUploadInputStreamOperation.kt @@ -116,6 +116,7 @@ class AWSS3StorageUploadInputStreamOperation @JvmOverloads internal constructor( objectMetadata.metaData[ObjectMetadata.SERVER_SIDE_ENCRYPTION] = storageServerSideEncryption.getName() } + objectMetadata.metaData[ObjectMetadata.STORAGE_CLASS] = uploadRequest.storageClass.value transferObserver = storageService.uploadInputStream( transferId, serviceKey, diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions.java b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions.java index 39dfdcaf4..69d3fe807 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions.java +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/options/AWSS3StorageUploadFileOptions.java @@ -23,16 +23,20 @@ import java.util.Objects; +import aws.sdk.kotlin.services.s3.model.StorageClass; + /** * Options to specify attributes of object upload operation to an AWS S3 bucket. */ public final class AWSS3StorageUploadFileOptions extends StorageUploadFileOptions { private final ServerSideEncryption serverSideEncryption; + private final StorageClass storageClass; private final boolean useAccelerationMode; private AWSS3StorageUploadFileOptions(final Builder builder) { super(builder); this.serverSideEncryption = builder.getServerSideEncryption(); + this.storageClass = builder.getStorageClass(); this.useAccelerationMode = builder.useAccelerateEndpoint; } @@ -45,6 +49,15 @@ public ServerSideEncryption getServerSideEncryption() { return serverSideEncryption; } + /** + * Storage class. + * @return Storage class + */ + @NonNull + public StorageClass getStorageClass() { + return storageClass; + } + /** * Factory method to create a new instance of the * {@link Builder}. The builder can be @@ -75,6 +88,7 @@ public static Builder from(@NonNull final AWSS3StorageUploadFileOptions options) .targetIdentityId(options.getTargetIdentityId()) .contentType(options.getContentType()) .serverSideEncryption(options.getServerSideEncryption()) + .storageClass(options.getStorageClass()) .metadata(options.getMetadata()); } @@ -109,6 +123,7 @@ public boolean equals(Object obj) { ObjectsCompat.equals(getTargetIdentityId(), that.getTargetIdentityId()) && ObjectsCompat.equals(getContentType(), that.getContentType()) && ObjectsCompat.equals(getServerSideEncryption(), that.getServerSideEncryption()) && + ObjectsCompat.equals(getStorageClass(), that.getStorageClass()) && ObjectsCompat.equals(getMetadata(), that.getMetadata()); } } @@ -121,6 +136,7 @@ public int hashCode() { getTargetIdentityId(), getContentType(), getServerSideEncryption(), + getStorageClass(), getMetadata() ); } @@ -134,6 +150,7 @@ public String toString() { ", targetIdentityId=" + getTargetIdentityId() + ", contentType=" + getContentType() + ", serverSideEncryption=" + getServerSideEncryption().getName() + + ", storageClass=" + getStorageClass() + ", metadata=" + getMetadata() + '}'; } @@ -145,11 +162,13 @@ public String toString() { */ public static final class Builder extends StorageUploadFileOptions.Builder { private ServerSideEncryption serverSideEncryption; + private StorageClass storageClass; private boolean useAccelerateEndpoint; private Builder() { super(); this.serverSideEncryption = ServerSideEncryption.NONE; + this.storageClass = StorageClass.Standard.INSTANCE; } /** @@ -173,11 +192,27 @@ public Builder serverSideEncryption(@NonNull ServerSideEncryption serverSideEncr return this; } + /** + * Configures the storage class for a new AWSS3StorageUploadFileOptions instance. + * @param storageClass storage class + * @return Current Builder instance for fluent chaining + */ + @NonNull + public Builder storageClass(@NonNull StorageClass storageClass) { + this.storageClass = Objects.requireNonNull(storageClass); + return this; + } + @NonNull ServerSideEncryption getServerSideEncryption() { return serverSideEncryption; } + @NonNull + StorageClass getStorageClass() { + return storageClass; + } + @Override @NonNull public AWSS3StorageUploadFileOptions build() { diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions.java b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions.java index e14296a2c..c50ef3954 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions.java +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/options/AWSS3StorageUploadInputStreamOptions.java @@ -23,16 +23,20 @@ import java.util.Objects; +import aws.sdk.kotlin.services.s3.model.StorageClass; + /** * Options to specify attributes of object upload operation to an AWS S3 bucket. */ public final class AWSS3StorageUploadInputStreamOptions extends StorageUploadInputStreamOptions { private final ServerSideEncryption serverSideEncryption; + private final StorageClass storageClass; private final boolean useAccelerationMode; private AWSS3StorageUploadInputStreamOptions(final Builder builder) { super(builder); this.serverSideEncryption = builder.serverSideEncryption; + this.storageClass = builder.storageClass; this.useAccelerationMode = builder.useAccelerateEndpoint; } @@ -45,6 +49,15 @@ public ServerSideEncryption getServerSideEncryption() { return serverSideEncryption; } + /** + * Storage class. + * @return Storage class + */ + @NonNull + public StorageClass getStorageClass() { + return storageClass; + } + /** * Factory method to create a new instance of the * {@link Builder}. The builder can be @@ -75,6 +88,7 @@ public static Builder from(@NonNull final AWSS3StorageUploadInputStreamOptions o .targetIdentityId(options.getTargetIdentityId()) .contentType(options.getContentType()) .serverSideEncryption(options.getServerSideEncryption()) + .storageClass(options.getStorageClass()) .metadata(options.getMetadata()); } @@ -109,6 +123,7 @@ public boolean equals(Object obj) { ObjectsCompat.equals(getTargetIdentityId(), that.getTargetIdentityId()) && ObjectsCompat.equals(getContentType(), that.getContentType()) && ObjectsCompat.equals(getServerSideEncryption(), that.getServerSideEncryption()) && + ObjectsCompat.equals(getStorageClass(), that.getStorageClass()) && ObjectsCompat.equals(getMetadata(), that.getMetadata()); } } @@ -121,6 +136,7 @@ public int hashCode() { getTargetIdentityId(), getContentType(), getServerSideEncryption(), + getStorageClass(), getMetadata() ); } @@ -134,6 +150,7 @@ public String toString() { ", targetIdentityId=" + getTargetIdentityId() + ", contentType=" + getContentType() + ", serverSideEncryption=" + getServerSideEncryption().getName() + + ", storageClass=" + getStorageClass() + ", metadata=" + getMetadata() + '}'; } @@ -145,11 +162,13 @@ public String toString() { */ public static final class Builder extends StorageUploadInputStreamOptions.Builder { private ServerSideEncryption serverSideEncryption; + private StorageClass storageClass; private boolean useAccelerateEndpoint; private Builder() { super(); this.serverSideEncryption = ServerSideEncryption.NONE; + this.storageClass = StorageClass.Standard.INSTANCE; } /** @@ -173,6 +192,17 @@ public Builder serverSideEncryption(@NonNull ServerSideEncryption serverSideEncr return this; } + /** + * Configures the storage class for a new AWSS3StorageUploadInputStreamOptions instance. + * @param storageClass storage class + * @return Current Builder instance for fluent chaining + */ + @NonNull + public Builder storageClass(@NonNull StorageClass storageClass) { + this.storageClass = Objects.requireNonNull(storageClass); + return this; + } + @Override @NonNull public AWSS3StorageUploadInputStreamOptions build() { diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/request/AWSS3StoragePathUploadRequest.kt b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/request/AWSS3StoragePathUploadRequest.kt index 0c86b78db..954adc216 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/request/AWSS3StoragePathUploadRequest.kt +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/request/AWSS3StoragePathUploadRequest.kt @@ -14,6 +14,7 @@ */ package com.amplifyframework.storage.s3.request +import aws.sdk.kotlin.services.s3.model.StorageClass import com.amplifyframework.storage.StoragePath import com.amplifyframework.storage.s3.ServerSideEncryption @@ -26,6 +27,7 @@ internal data class AWSS3StoragePathUploadRequest( val local: L, val contentType: String?, val serverSideEncryption: ServerSideEncryption, + val storageClass: StorageClass, val metadata: Map, val useAccelerateEndpoint: Boolean ) diff --git a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/request/AWSS3StorageUploadRequest.java b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/request/AWSS3StorageUploadRequest.java index 516e30da7..cd03853c1 100644 --- a/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/request/AWSS3StorageUploadRequest.java +++ b/aws-storage-s3/src/main/java/com/amplifyframework/storage/s3/request/AWSS3StorageUploadRequest.java @@ -24,6 +24,8 @@ import java.util.HashMap; import java.util.Map; +import aws.sdk.kotlin.services.s3.model.StorageClass; + /** * Parameters to provide to S3 that describe a request to upload. * @param object to upload (e.g. File or InputStream) @@ -39,6 +41,7 @@ public final class AWSS3StorageUploadRequest { private final String targetIdentityId; private final String contentType; private final ServerSideEncryption serverSideEncryption; + private final StorageClass storageClass; private final Map metadata; private final boolean useAccelerateEndpoint; @@ -54,6 +57,7 @@ public final class AWSS3StorageUploadRequest { * If null, the operation will fetch the current identity ID. * @param contentType The standard MIME type describing the format of the object to store * @param serverSideEncryption server side encryption type for the current storage bucket + * @param storageClass The storage class to use for the object to store * @param metadata Metadata for the object to store * @param useAccelerateEndpoint flag to use acceleration endpoint. */ @@ -65,6 +69,7 @@ public AWSS3StorageUploadRequest( @Nullable String targetIdentityId, @Nullable String contentType, @NonNull ServerSideEncryption serverSideEncryption, + @NonNull StorageClass storageClass, @Nullable Map metadata, boolean useAccelerateEndpoint ) { @@ -74,6 +79,7 @@ public AWSS3StorageUploadRequest( this.targetIdentityId = targetIdentityId; this.contentType = contentType; this.serverSideEncryption = serverSideEncryption; + this.storageClass = storageClass; this.metadata = new HashMap<>(); if (metadata != null) { this.metadata.putAll(metadata); @@ -136,6 +142,15 @@ public ServerSideEncryption getServerSideEncryption() { return serverSideEncryption; } + /** + * Gets the storage class. + * @return storage class + */ + @NonNull + public StorageClass getStorageClass() { + return storageClass; + } + /** * Gets the metadata. * @return metadata