【问题标题】:How to Specify Storing Path in AWS Amplify in Android.?如何在 Android 的 AWS Amplify 中指定存储路径。?
【发布时间】:2020-05-15 00:26:51
【问题描述】:

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

我参考了 Amplify 存储的 JavaScriptAndroid 文档。

这是我的代码。

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

【问题讨论】:

  • 您好,欢迎在这里快速浏览stackoverflow.com/tour
  • 请提供您的实现代码以及到目前为止您尝试了什么
  • @AshwiniViolet 谢谢重播。你现在能请教我的问题吗。我已经编辑了

标签: android amazon-s3 aws-amplify


【解决方案1】:

如果要将文件上传到特定文件夹,则只需将文件夹名称路径前缀添加到方法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);
        }
    }
);

额外

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

【讨论】:

  • 非常感谢。工作完美。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2012-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多