android - 如何在 Android 的 AWS Amplify 中指定存储路径。?

标签 android amazon-s3 aws-amplify

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

我同时引用了 JavaScriptAndroid Amplify 存储的文档。

这是我的代码。

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() 的第一个关键参数中.

例如

假设您想将文件上传到名为“游戏”的文件夹中。

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

额外

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

关于android - 如何在 Android 的 AWS Amplify 中指定存储路径。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59968236/

相关文章:

Android webrtc 不适用于 3g、4g

C# S3 API DeleteObject 抛出 409 冲突 : The bucket your tried to delete is not empty

amazon-web-services - 无法使用 Amazon Amplify 从 Lambda 调用 GraphQL

android - AWS Amplify - Android - 将未知语言的文本翻译成英语

node.js - 无权使用联合身份从 Amplify graphQL 访问类型 Y 上的 X 未经授权的用户角色

android - 使用多个电话号码更新联系人

android - 显示对话框代码未执行

android - zxing 如何从文件中解码?

java - 使用 AmazonS3Client java 在 S3 上上传文件

ruby - 使用 Ruby 文件设置内容类型创建的文件?