1

我正在尝试制作一个将图像上传到 S3 服务的应用程序。从我在文档和示例项目中读到的内容,我看到创建一个存储桶并将图像上传到它是相当容易的,这没问题,但是我如何测试存储桶是否已经存在并连接到它如果是吗?

我的代码取自亚马逊示例项目,但无论如何我都会添加它:

    case R.id.button_upload:
        if (flag && fileUri != null) {
            Uri selectedImage = fileUri;
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();

            // Put the image data into S3.
            try {
                s3Client.createBucket(Constants.getPictureBucket());
                // I DON"T WANT TO CREATE ONE, I WANT TO CONNECT TO ONE

                PutObjectRequest por = new PutObjectRequest(
                        Constants.getPictureBucket(),
                        Constants.PICTURE_NAME, new java.io.File(filePath)); // Content
                                                                                // type
                                                                                // is
                                                                                // determined
                                                                                // by
                                                                                // file
                                                                                // extension.
                s3Client.putObject(por);
            } catch (Exception exception) {
                displayAlert("Upload Failure", exception.getMessage());
            }
        }
        break;

关于如何连接到现有存储桶的任何想法?

4

1 回答 1

2

您不应该创建很多存储桶。一个就足够了,然后里面只有文件夹。

 String bucket = "foo";
 File file = new File(fileName);
 //Just set your directory 
 PutObjectRequest request = new PutObjectRequest(bucket,"thumbnails/" +  fileName, file)
 .withCannedAcl(CannedAccessControlList.PublicRead)
 .withMetadata(meta);
 client.putObject(request);
于 2012-05-07T09:27:02.100 回答