File access levels
When adding the Storage category, you configure the level of access users have to your S3 bucket. You can configure separate rules for authenticated vs. guest users. When using the Storage category to upload files, you can also specify an access level for each individual file: guest, protected, or private.
- Guest Accessible by all users of your application
- Protected Readable by all users, but only writable by the creating user
- Private Readable and writable only by the creating user
Guest access does not mean that your files are totally public. A "guest" is a user of your application who has not yet signed in. To enable access at this level, you will still be required to configured Authentication in your app. The user must be able to assume an unauthenticated role from your Cognito Identity Pool.
For protected and private access, the [IDENTITY_ID]
below corresponds to the unique ID of the user. Once the user has signed in, the [IDENTITY_ID]
can be retrieved from the session by accessing the identity id. See Accessing credentials to retrieve the identity id, and use this as the unique ID of the authenticated user.
Protected access
After the user has signed in, create an options object specifying the protected
access level to allow other users to read the object:
private void uploadFile(String key, File file) { StorageUploadFileOptions options = StorageUploadFileOptions.builder() .accessLevel(StorageAccessLevel.PROTECTED) .build();
Amplify.Storage.uploadFile(key, file, options, result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + key), error -> Log.e("MyAmplifyApp", "Upload failed", error) );}
private fun uploadFile(key: String, file: File) { val options = StorageUploadFileOptions.builder() .accessLevel(StorageAccessLevel.PROTECTED) .build() Amplify.Storage.uploadFile(key, file, options, { Log.i("MyAmplifyApp", "Successfully uploaded: $key" ) }, { error -> Log.e("MyAmplifyApp", "Upload failed", error) } )}
private suspend fun uploadFile(key: String, file: File) { val options = StorageUploadFileOptions.builder() .accessLevel(StorageAccessLevel.PROTECTED) .build() val upload = Amplify.Storage.uploadFile(key, file, options) try { upload.result() Log.i("MyAmplifyApp", "Successfully uploaded: $key") } catch (error: StorageException) { Log.e("MyAmplifyApp", "Upload failed", error) }}
private void uploadFile(String key, File file) { StorageUploadFileOptions options = StorageUploadFileOptions.builder() .accessLevel(StorageAccessLevel.PROTECTED) .build();
RxProgressAwareSingleOperation<StorageUploadFileResult> upload = RxAmplify.Storage.uploadFile("ExampleKey", exampleFile, options);
upload .observeResult() .subscribe( result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()), error -> Log.e("MyAmplifyApp", "Upload failed", error) );}
This will upload with the prefix /protected/[IDENTITY_ID]/
followed by the key
.
For other users to read the file, you must specify the access level as protected
and the identity ID of the user who uploaded it in the options.
private void downloadFile(File file, String key, String otherIdentityId) { StorageDownloadFileOptions options = StorageDownloadFileOptions.builder() .accessLevel(StorageAccessLevel.PROTECTED) .targetIdentityId(otherIdentityId) .build();
Amplify.Storage.downloadFile(key, file, options, result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + key), error -> Log.e("MyAmplifyApp", "Download failed", error) );}
private fun downloadFile(file: File, key: String, otherIdentityId: String) { val options = StorageDownloadFileOptions.builder() .accessLevel(StorageAccessLevel.PROTECTED) .targetIdentityId(otherIdentityId) .build() Amplify.Storage.downloadFile(key, file, options, { Log.i("MyAmplifyApp", "Successfully downloaded: $key") }, { error -> Log.e("MyAmplifyApp", "Download failed", error) } )}
private suspend fun downloadFile(file: File, key: String, otherIdentityId: String) { val options = StorageDownloadFileOptions.builder() .accessLevel(StorageAccessLevel.PROTECTED) .targetIdentityId(otherIdentityId) .build() val download = Amplify.Storage.downloadFile(key, file, options) try { download.result() Log.i("MyAmplifyApp", "Successfully downloaded: $key") } catch (error: StorageException) { Log.e("MyAmplifyApp", "Download failed", error) }}
Private Access
Create an options object specifying the private access level to only allow an object to be accessed by the creating user
private void uploadFile(String key, File file) { StorageUploadFileOptions options = StorageUploadFileOptions.builder() .accessLevel(StorageAccessLevel.PRIVATE) .build();
Amplify.Storage.uploadFile(key, file, options, result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + key), error -> Log.e("MyAmplifyApp", "Upload failed", error) );}
private fun uploadFile(key: String, file: File) { val options = StorageUploadFileOptions.builder() .accessLevel(StorageAccessLevel.PRIVATE) .build() Amplify.Storage.uploadFile(key, file, options, { Log.i("MyAmplifyApp", "Successfully uploaded: $key") }, { error -> Log.e("MyAmplifyApp", "Upload failed", error) } )}
private suspend fun uploadFile(key: String, file: File) { val options = StorageUploadFileOptions.builder() .accessLevel(StorageAccessLevel.PRIVATE) .build()
val upload = Amplify.Storage.uploadFile(key, file, options) try { upload.result() Log.i("MyAmplifyApp", "Successfully uploaded: $key") } catch (error: StorageException) { Log.e("MyAmplifyApp", "Upload failed", error) }}
This will upload with the prefix /private/[IDENTITY_ID]/
, followed by the key
.
For the user to read the file, specify the same access level (private
) and key you used to upload:
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder() .accessLevel(StorageAccessLevel.PRIVATE) .build();
val options = StorageDownloadFileOptions.builder() .accessLevel(StorageAccessLevel.PRIVATE) .build()
Customization
Customize Object Key Path
You can customize your key path by defining a prefix resolver:
// Define your own prefix resolver, that conforms to `AWSS3StoragePluginPrefixResolver`public class MyDeveloperPrefixResolver implements AWSS3PluginPrefixResolver {
@Override public void resolvePrefix(@NonNull StorageAccessLevel accessLevel, @Nullable String targetIdentity, @NonNull Consumer<String> onSuccess, @NonNull Consumer<StorageException> onError) { switch (accessLevel) { case PUBLIC: { onSuccess.accept("myPublicPrefix/"); } case PROTECTED: { onSuccess.accept("myProtectedPrefix/" + targetIdentity + "/"); } case PRIVATE: { onSuccess.accept("myPrivatePrefix/" + targetIdentity + "/"); } } }}
// Define your own prefix resolver, that conforms to `AWSS3StoragePluginPrefixResolver`class MyDeveloperPrefixResolver: AWSS3PluginPrefixResolver {
override fun resolvePrefix( accessLevel: StorageAccessLevel, targetIdentity: String?, onSuccess: Consumer<String>, onError: Consumer<StorageException> ) { when(accessLevel) { StorageAccessLevel.PUBLIC -> { onSuccess.accept("myPublicPrefix/") } StorageAccessLevel.PROTECTED -> { onSuccess.accept("myProtectedPrefix/$targetIdentity/") } StorageAccessLevel.PRIVATE -> { onSuccess.accept("myPrivatePrefix/$targetIdentity/") } } }}
Then configure the storage plugin with the resolver.
AWSS3StoragePluginConfiguration.Builder builder = new AWSS3StoragePluginConfiguration.Builder();builder.setAwsS3PluginPrefixResolver(new MyDeveloperPrefixResolver());Amplify.addPlugin(new AWSS3StoragePlugin(builder.build()));
Amplify.addPlugin<Plugin<*>>( AWSS3StoragePlugin( AWSS3StoragePluginConfiguration { awsS3PluginPrefixResolver = MyDeveloperPrefixResolver() } ))
Add the IAM policy that corresponds with the prefixes defined above to enable read, write and delete operation for all the objects under path myPublicPrefix/:
"Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::your-s3-bucket/myPublicPrefix/*", ] }]
If you want to have custom private path prefix like myPrivatePrefix/, you need to add it into your IAM policy:
"Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:PutObject", "s3:DeleteObject" ], "Resource": [ "arn:aws:s3:::your-s3-bucket/myPrivatePrefix/${cognito-identity.amazonaws.com:sub}/*" ] }]
Passthrough PrefixResolver
If you would like no prefix resolution logic, such as performing S3 requests at the root of the bucket, create a prefix resolver that returns an empty string:
public class PassThroughPrefixResolver implements AWSS3PluginPrefixResolver {
@Override public void resolvePrefix(@NonNull StorageAccessLevel accessLevel, @Nullable String targetIdentity, @NonNull Consumer<String> onSuccess, @NonNull Consumer<StorageException> onError) { onSuccess.accept("") }}
class PassThroughPrefixResolver: AWSS3PluginPrefixResolver {
override fun resolvePrefix( accessLevel: StorageAccessLevel, targetIdentity: String?, onSuccess: Consumer<String>, onError: Consumer<StorageException> ) { onSuccess.accept("") }}