访问现有的Amazon S3存储桶

时间:2012-05-07 09:23:06

标签: android amazon-s3

我正在尝试制作一个将图片上传到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;

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

1 个答案:

答案 0 :(得分: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);