【发布时间】:2016-12-17 13:45:16
【问题描述】:
原始问题
我已经按照您的建议进行了尝试,我遇到的问题很少。 以下是我所做的:filePath = Capture.capturePhoto(1024, -1);
我在 MutipartRequest 构造函数中传递 S3_BUCKET_URL 时遇到问题,因此我使用了 rq.setUrl(S3_BUCKET_URL)
我必须添加 rq.setHttpMethod("PUT"); // 因为我收到 405 错误:不支持方法
最后我没有发现任何错误,我确实看到在我创建的存储桶下创建了一个子文件夹。我将网址设置为“https://s3.amazonaws.com/bucket123/test/”我看到创建了测试存储桶,但没有上传图像文件。文件夹大小不是 0,因为它显示了文件的大小,但在该子文件夹中没有找到图像文件。
当我尝试通过 S3 资源管理器访问该文件夹时,我得到了拒绝访问。 1.我使用S3资源管理器手动创建了一个子文件夹并授予了读写权限,但是一旦从代号调用uploadFileToS3方法,我看到权限丢失了。 2. 所以我把acl改成“public-read-write”还是一样的效果。
如果我遗漏了什么,请告知。
谢谢。
修改后的问题 我已经修改了旧问题,并按照 Sahi 的回答进行了修改。
// Modified the url as following - full path including bucket name and key
private static final String S3_BUCKET_URL = "https://s3.amazonaws.com/my-bucket/myfile.jpg";
//String filePath = captured from Camera;
public void uploadFileToS3(String filePath, String uniqueFileName) {
MultipartRequest rq = new MultipartRequest();
rq.setUrl(S3_BUCKET_URL);
//rq.addArgument("acl", "private");
//rq.addArgument("key", uniqueFileName);
rq.addData(uniqueFileName, filePath, "image/jpeg");
rq.setReadResponseForErrors(true);
NetworkManager.getInstance().addToQueue(rq);
}
Now I do see that the file has been uploaded to the bucket as myfile.jpg under my bucket. The bucket has all the rights since the bucket is given rights to read and write also applied the permissions to sub folders.
Now the myfile.jpg lost all the permissions and I am not able to download that file even though I see it in the bucket.
I accessed it by the url but the file cannot be opened.
I am not sure what I am missing in the header or request. I am really trying to get this working but not getting where I wanted to. This is very important piece for the app I am working on.
Please provide any feedback.
Thank you.
--------------------------------------------------------------------------
添加代码
私有静态最终字符串 S3_BUCKET_URL = "https://s3.amazonaws.com/my-bucket/";
当我拥有上述 url 时,my-bucket 被创建为未知对象(没有文件夹图标),但我确实看到文件大小是实际图像大小。
//String filePath = captured from Camera;
public void uploadFileToS3(String filePath, String uniqueFileName) {
//uniqueFileName including sub folder - subfolder/my-image.jpg
MultipartRequest rq = new MultipartRequest();
rq.setUrl(S3_BUCKET_URL);
rq.addArgument("acl", "public-read-write");
rq.addArgument("key", uniqueFileName);
rq.addData(uniqueFileName, filePath, "image/jpeg");
rq.setReadResponseForErrors(true);
NetworkManager.getInstance().addToQueue(rq);
}
看起来我没有正确上传对象。我确实浏览了亚马逊文档,但找不到与通过 http 上传相关的任何内容。我真的被困住了,我希望能解决这个问题。您是否有任何可以简单地上传到 s3 的工作代码?
More details: I am adding more detail to the question as I am still not able to resolve this.
// Original S3_BUCKET_URL
String S3_BUCKET_URL = "https://s3.amazonaws.com/my-bucket/";
// With this url I am getting 400 : Bad Request Error
// I added the uniqueFilename (key) to the url as
String uniqueFilename = "imageBucket/image12345.jpg";
String S3_BUCKET_URL = "https://s3.amazonaws.com/my-bucket /"+uniqueFilename;
//Now I do see the file I believe it's the file, the
//subfolder inherits the rights from bucket (read, write).
//Not the image file.
//When I click on the file using s3 client browser I got message
//prompted Access Denied.
// my function to call s3 bucket
public void takePicture()
{
String stringImg = Capture.capturePhoto(1024, -1);
MultipartRequest rq = new MultipartRequest();
rq.setUrl(S3_BUCKET_URL);
rq.addArgument("key", uniqueFilename );
rq.addArgument("acl", "public-read-write");
rq.setHttpMethod("PUT"); // If I don't specify this I am getting
// 405 : Method Not Allowed Error.
rq.addData("file", stringImg, "image/jpeg");
//Captured Image from camera
rq.setFilename("file", uniqueFilename);
NetworkManager.getInstance().addToQueue();
}
在这一点上,我相信我已经完成了所有建议的方式,但我仍然遇到同样的错误。我相信我没有做错任何事,我不想相信我是唯一一个试图这样做的人:)
我真的需要你的帮助来解决这个问题,以保持这个项目。我想知道是否有任何 Amazon s3 sdk 或库可以在代号一中使用。
请查看此内容并让我知道这是否真的可以通过代号一来实现。
谢谢。
【问题讨论】:
-
另外,当我的网址为 S3_BUCKET_URL = "s3.amazonaws.com/my-bucket/";我收到 400 错误:错误请求
标签: amazon-web-services amazon-s3 codenameone