What is Amazon S3?
Amazon Simple Storage Service => storage for the Internet, you can use it to store and retrieve data on the web.
Sections to do ….
| num | Information | Relevant sections |
|---|---|---|
| 1. | General product overview and pricing | Amazon S3 |
| 2. | How do I work with buckets? | Buckets overview |
| 3. | How do I work with access points? | Managing data access with Amazon S3 access points |
| 4. | How do I work with objects? | Amazon S3 objects overview |
| 5. | How do I make requests? | Making requests |
| 6. | How do I manage access to my resources? | Identity and access management in Amazon S3 |
Why to use Amazon S3?
By using Amazon S3 we can easily create buckets, storing data and download data with permissions.
How to start using Amazon S3?
Step 1:
Abb this command to commandLine: amplify add storage;
Then Enter the following:
? Please select from one of the below mentioned services:
`Content (Images, audio, video, etc.)`
? You need to add auth (Amazon Cognito) to your project in order to add storage for user files. Do you want to add auth now?
`Yes`
? Do you want to use the default authentication and security configuration?
`Default configuration`
? How do you want users to be able to sign in?
`Username`
? Do you want to configure advanced settings?
`No, I am done.`
? Please provide a friendly name for your resource that will be used to label this category in the project:
`S3friendlyName`
? Please provide bucket name:
`storagebucketname`
? Who should have access:
`Auth and guest users`
? What kind of access do you want for Authenticated users?
`create/update, read, delete`
? What kind of access do you want for Guest users?
`create/update, read, delete`
? Do you want to add a Lambda Trigger for your S3 Bucket?
`No`
Step 2:
Push the changes to amplify: amplify push
Step 3:
Add the dependencies:
dependencies {
implementation 'com.amplifyframework:aws-storage-s3:1.17.4'
implementation 'com.amplifyframework:aws-auth-cognito:1.17.4'
}
Step 4:
Initialize Amplify Storage
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSS3StoragePlugin());
Then the class should look like this:
public class MyAmplifyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
try {
// Add these lines to add the AWSCognitoAuthPlugin and AWSS3StoragePlugin plugins
Amplify.addPlugin(new AWSCognitoAuthPlugin());
Amplify.addPlugin(new AWSS3StoragePlugin());
Amplify.configure(getApplicationContext());
Log.i("MyAmplifyApp", "Initialized Amplify");
} catch (AmplifyException error) {
Log.e("MyAmplifyApp", "Could not initialize Amplify", error);
}
}
}
Step 5:
Upload data to your bucket
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "ExampleKey");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
Amplify.Storage.uploadFile(
"ExampleKey",
exampleFile,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}