java - 使用 uploadDirectory 从 android 到 S3 的示例

标签 java android amazon-web-services amazon-s3

有人可以提供使用 uploadDirectory 将一堆照片上传到 S3 的示例吗?假设我在我的 Android 设备上名为“special_photos”的目录中有 300 张照片。我想将所有这些照片上传到 Amazon S3。我认为 uploadDirectory 可能是执行此操作的最佳方法。但是作为亚马逊云的新手,我不知道我该怎么做。到目前为止我所收集到的是该方法异步执行,因此可以从主线程调用。我一直在互联网上寻找 php 代码。但我不使用 PHP。有没有人有他们不介意与社区分享的完整工作示例?我在 Android Studio 上通过 gradle 使用 SDK。另外,是否有某种回调可以知道何时上传了所有照片?比如说我想在上传照片和目录后将其删除。

最佳答案

没有 uploadDirectory 但有 Multipart Upload .这会将您的大数据上传到 S3。如前所述HERE ,分段上传文档说:

Using the list multipart uploads operation, you can obtain a list of multipart uploads in progress. An in-progress multipart upload is an upload that you have initiated, but have not yet completed or aborted. Each request returns at most 1000 multipart uploads. If there are more than 1000 multipart uploads in progress, you need to send additional requests to retrieve the remaining multipart uploads.

为了解决回调问题,一旦所有 TransferUtility 项目都上传完毕,就会调用完成。这open source添加应用于上传功能的监听器。我建议一次将您的通话分成 30 个,然后删除相应的照片 - 以防上传失败。有成功和失败返回,所以显然只有在成功的情况下才删除。

HERE是 Android 分段上传的 AWS 文档

HERE是一篇有助于迁移和理解 TransferManagerTransferUtility

之间差异的文章

HERE是一篇关于开始使用 Android TransferManager

的好文章

HERE是一个开源演示 - 在 S3_TransferManager

希望这对您有所帮助!

更新:

以下代码全部取自@awslabs 引用资料

创建客户端:

public static AmazonS3Client getS3Client(Context context) {
    if (sS3Client == null) {
        sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()));
    }
    return sS3Client;
}

创建 TransferUtility:

public static TransferUtility getTransferUtility(Context context) {
    if (sTransferUtility == null) {
        sTransferUtility = new TransferUtility(getS3Client(context.getApplicationContext()),
                context.getApplicationContext());
    }
    return sTransferUtility;
}

使用 TransferUtility 获取所有上传传输:

observers = transferUtility.getTransfersWithType(TransferType.UPLOAD);

添加您的记录: - 您可以遍历目录中的文件名

HashMap<String, Object> map = new HashMap<String, Object>();
Util.fillMap(map, observer, false);
transferRecordMaps.add(map);

一切由此开始:

private void beginUpload(String filePath) {
    if (filePath == null) {
        Toast.makeText(this, "Could not find the filepath of the selected file",
                Toast.LENGTH_LONG).show();
        return;
    }
    File file = new File(filePath);
    TransferObserver observer = transferUtility.upload(Constants.BUCKET_NAME, file.getName(),
            file);
    observers.add(observer);
    HashMap<String, Object> map = new HashMap<String, Object>();
    Util.fillMap(map, observer, false);
    transferRecordMaps.add(map);
    observer.setTransferListener(new UploadListener());
    simpleAdapter.notifyDataSetChanged();
}

这是你的完成:

private class GetFileListTask extends AsyncTask<Void, Void, Void> {
    // The list of objects we find in the S3 bucket
    private List<S3ObjectSummary> s3ObjList;
    // A dialog to let the user know we are retrieving the files
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(DownloadSelectionActivity.this,
                getString(R.string.refreshing),
                getString(R.string.please_wait));
    }

    @Override
    protected Void doInBackground(Void... inputs) {
        // Queries files in the bucket from S3.
        s3ObjList = s3.listObjects(Constants.BUCKET_NAME).getObjectSummaries();
        transferRecordMaps.clear();
        for (S3ObjectSummary summary : s3ObjList) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("key", summary.getKey());
            transferRecordMaps.add(map);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
        simpleAdapter.notifyDataSetChanged();
    }
}

关于java - 使用 uploadDirectory 从 android 到 S3 的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890684/

相关文章:

android - 线程安全 RNG Android NDK

amazon-web-services - 亚马逊 Lex 错误 : An error occurred (BadRequestException) when calling the PutIntent operation: RelativeId does not match Lex ARN format

python - 构建私有(private) API 来连接移动应用程序?

java - 在 Java 中的 JButton 中有 JCheckBox 可以吗?

java - 如何将参数传递给 Play!框架 1.2.x?

android - ACTION_POWER_CONNECTED 的问题

java - 在 Android 中访问 Earthquake API。未知主机异常

java - HTTP请求参数类型传播

java - Java的Timer任务保证不会并发运行吗?

amazon-web-services - 将索引模板上传到 aws elasticsearch 服务