AWS Amplify in Android中如何指定存储路径?

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

我可以使用 AWS Amplify Storage 类别存储文件。但是,它们都存储在我存储桶中 public 文件夹的顶部。如何在 public 文件夹中指定路径?

我参考了 Amplify 存储的 JavaScript and Android 文档。

这是我的代码。

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);
        }
    }
);

如果你想上传一个文件到一个特定的文件夹,那么你所要做的就是将文件夹名称路径前缀添加到方法的第一个关键参数Amplify.Storage.uploadFile()

例如

假设您要将文件上传到名称为 "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);
        }
    }
);

额外

对于下载、删除等其他方法,您必须执行相同的操作才能访问这些文件。只需添加前缀。