AWS S3 - JamesDansie/data-structures-and-algorithms GitHub Wiki

AWS S3

Author: James Dansie

S3 is a cloud storage service from AWS. It can be accessed programmatically through your apps, just like a file reader system. Each file will then have its own url, that can be linked to. The storage terms are pretty generous "Upon sign-up, new AWS customers receive 5 GB of Amazon S3 Standard storage, 20,000 Get Requests, 2,000 Put Requests, 15GB of data transfer in, and 15GB of data transfer out each month for one year." [1] Authentication can either be in a local file at ~/.aws/credentials or declared in the server in an env variable (for JS);

const s3 = new aws.S3({
  accessKeyId: process.env.aws_access_key_id,
  secretAccessKey: process.env.aws_secret_access_key
});

Before you can upload a file to S3 you need the uri for that file. This is how the phone picks that file. It can be found from [4]. In particular you'll need;

private static final int READ_REQUEST_CODE = 42;
...
/**
 * Fires an intent to spin up the "file chooser" UI and select an image.
 */
public void performFileSearch() {

    // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
    // browser.
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

    // Filter to only show results that can be "opened", such as a
    // file (as opposed to a list of contacts or timezones)
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Filter to show only images, using the image MIME data type.
    // If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
    // To search for all documents available via installed storage providers,
    // it would be "*/*".
    intent.setType("image/*");

    startActivityForResult(intent, READ_REQUEST_CODE);
}

and

@Override
public void onActivityResult(int requestCode, int resultCode,
        Intent resultData) {

    // The ACTION_OPEN_DOCUMENT intent was sent with the request code
    // READ_REQUEST_CODE. If the request code seen here doesn't match, it's the
    // response to some other intent, and the code below shouldn't run at all.

    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        // The document selected by the user won't be returned in the intent.
        // Instead, a URI to that document will be contained in the return intent
        // provided to this method as a parameter.
        // Pull that URI using resultData.getData().
        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();
            Log.i(TAG, "Uri: " + uri.toString());
        }
    }
}

For amplify [3];

  1. Enter the command;
 $ cd ./YOUR_PROJECT_FOLDER
 $ amplify add storage
  1. Choose Content as your storage service.
  2. Grant all users access; "(select Auth and guest users and toggle all to select create/update, read, and delete access for both auth and guest users)"[3].
  3. Double check with amplify status
  4. amplify push
  5. Add dependencies;
 dependencies {
   implementation 'com.amazonaws:aws-android-sdk-s3:2.15.+'
   implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.15.+@aar') { transitive = true }
   implementation ('com.amazonaws:aws-android-sdk-auth-userpools:2.15.+@aar') { transitive = true }
 }
  1. Update the AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android:enabled="true" />

And you're ready to write code! For sample code check [3].


References

  1. https://aws.amazon.com/s3/faqs/
  2. https://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html
  3. https://aws-amplify.github.io/docs/android/storage
  4. https://developer.android.com/guide/topics/providers/document-provider#client