我能够使用 AWS Amplify Storage 类别存储文件。但是,它们都存储在我存储桶中公共(public)文件夹的顶部。如何在公用文件夹中指定路径?
我同时引用了 JavaScript和 Android 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/