Search code examples
androidamazon-s3aws-amplify

How to Specify Storing Path in AWS Amplify in Android.?


I'm able to store files with the AWS Amplify Storage category. However, they all are being stored in the top of the public folder in my bucket. How do I specify a path inside the public folder?

I referenced both the JavaScript and Android documentation for Amplify storage.

Here's my code.

Amplify.Storage.uploadFile(
    "filenmae.txt",
    filename.getAbsolutePath(),
    new ResultListener<StorageUploadFileResult>() {
        @Override
        public void onResult(StorageUploadFileResult result) {
            Log.i("StorageQuickStart", "Successfully uploaded: " + result.getKey());
        }

        @Override
        public void onError(Throwable error) {
            Log.e("StorageQuickstart", "Upload error.", error);
        }
    }
);

Solution

  • If you want to upload A file to a specific folder, then all you have to do is add the folder name path prefix to your 1st key parameter of the method Amplify.Storage.uploadFile().

    For Example

    let's say you want to upload your files in a folder that have name "game".

    // Name of your folder with '/' in the end to make it like path prefix
    String folderGame = "game/";
    
    // here we just adding it before the name of your file 
    String key = folderGame +"filenmae.txt";            
    
    /*
     * Now the value in key will look like "game/filenmae.txt" and 
     * pass it in method as first parameter where you were passing 
     * the name previously.
     */
    Amplify.Storage.uploadFile(
        key,
        filename.getAbsolutePath(),
        new ResultListener<StorageUploadFileResult>() {
            @Override
            public void onResult(StorageUploadFileResult result) {
                Log.i("StorageQuickStart", "Successfully uploaded: " + result.getKey());
            }
    
            @Override
            public void onError(Throwable error) {
                Log.e("StorageQuickstart", "Upload error.", error);
            }
        }
    );
    

    Extra

    For the other methods like download, remove etc., you have to do the same thing to access these files. Just add the prefix.