代号一将相机拍摄的照片上传到亚马逊s3存储桶 [英] Codename one Upload a picture taken by the camera to amazon s3 bucket

查看:69
本文介绍了代号一将相机拍摄的照片上传到亚马逊s3存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始问题

我已经按照您的建议进行了尝试,但我遇到的问题很少. 我做了以下工作:filePath = Capture.capturePhoto(1024,-1);

I have tried as you have suggested, there are few things I have run into few issues. Here are the following I did: filePath = Capture.capturePhoto(1024, -1);

我在MutipartRequest构造函数中传递S3_BUCKET_URL时遇到问题,所以我使用了rq.setUrl(S3_BUCKET_URL)

I had issue passing the S3_BUCKET_URL in the MutipartRequest Constructor so I used rq.setUrl(S3_BUCKET_URL)

我必须添加rq.setHttpMethod("PUT");//由于出现405错误,因此提示错误:不支持该方法

I had to add rq.setHttpMethod("PUT"); // since I got error as 405: Method was not supported

最后,我没有出现任何错误,并且确实看到在创建的存储桶下创建了一个子文件夹.我将网址设置为" https://s3.amazonaws.com/bucket123/test/我看到创建了测试存储区,但未上传图像文件.文件夹的大小不是0,因为它显示了文件的大小,但是在该子文件夹中没有找到图像文件.

Finally I got no errors and I did see a sub-folder created under the bucket I created. I set the url as "https://s3.amazonaws.com/bucket123/test/" I saw test bucket created but image files wasn't uploaded. The folder size wasn't 0 as it showed the size of the file but no image files were found in that sub folder.

当我尝试通过S3资源管理器访问该文件夹时,访问被拒绝. 1.我使用S3资源管理器手动创建了一个子文件夹,并赋予了读取和写入权限,但是一旦从代号中调用了uploadFileToS3方法,我看到该权限丢失了. 2.因此,我确实将acl更改为"public-read-write",效果仍然相同.

When I try to access the folder through S3 explorer I got the the Access Denied. 1. I manually created a sub-folder using S3 explorer and gave the read and write permission yet once the uploadFileToS3 method called from codename I saw the permissions was lost. 2. So I did change the acl to "public-read-write" still same effect.

请告知我是否有任何遗物.

Please advise if I am missing anything.

谢谢.

修改后的问题 我修改了旧的问题,并在此之后加上了Sahi的回答.

Modified question I have modified the old questions and following this with Sahi's answer.

    // 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/;

private static final String S3_BUCKET_URL = "https://s3.amazonaws.com/my-bucket/";

当我具有上面的URL时,我的存储桶被创建为未知对象(没有文件夹图标),但是我确实看到文件大小是实际图像大小.

When I had the url as above, the my-bucket was created as unknown object (no folder icon) but I did see the file size was actual image size.

//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的工作代码?

Looks like I am not uploading the object properly. I did go through the amazon document and I wasn't able to find anything related to upload through http. I am really stuck and I hope to get this resolved. Would you have any working code that simply uploads to 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();
    }

在这一点上,我相信我已经完成了所有建议的方法,但仍然遇到相同的错误.我相信我没有做错任何事情,并且我不想相信我是唯一尝试这样做的人:)

At this point I believe I have done all the suggested way and I am still getting the same error. I believe I am not doing anything wrong and I don't want to believe that I am the only one trying to do this :)

我真的需要您的帮助才能解决此问题,以便继续进行此项目.我想知道是否可以在代号一中使用任何Amazon s3 sdk或库.

I really need your help to get this resolved in order to keep this project. I am wondering if there is any Amazon s3 sdk or library I can use in codename one.

请仔细阅读此内容,并让我知道代号"1"是否真的可以实现.

Please review this and let me know if this really can be achieved by codename one.

谢谢.

推荐答案

我已经测试了此代码,它应该可以工作,但是它依赖于Codename One的修复程序来处理Amazon API的某些愚蠢行为:

I've tested this code and it should work however it depends on a fix to Codename One to deal with some stupid behavior of the Amazon API:

Form hi = new Form("Upload");

Button upload = new Button("Upload");
hi.add(upload);
upload.addActionListener(e -> {
    String file = Capture.capturePhoto();
    if(file != null) {
        try {
            String uniqueFileName = "Image" + System.currentTimeMillis() + ".jpg";
            MultipartRequest rq = new MultipartRequest();
            rq.setUrl("https://s3.amazonaws.com/my-bucket/");
            rq.addArgument("key", uniqueFileName);
            rq.addArgument("acl", "private");
            rq.addData("file", file, "image/jpeg");
            rq.setReadResponseForErrors(true);
            NetworkManager.getInstance().addToQueueAndWait(rq);            
            if(rq.getResponseCode() != 200 && rq.getResponseCode() != 204) {
                System.out.println("Error response: " + new String(rq.getResponseData()));
            }
            ToastBar.showMessage("Upload completed", FontImage.MATERIAL_INFO);
        } catch(IOException err) {
            Log.e(err);
        }
    }
});

hi.show();

运行下面的代码(带后文)时,我没有得到您所描述的错误,而是从Amazon收到了一个错误消息,指示密钥的顺序不正确.显然,亚马逊依赖提交的字段的顺序,这真是愚蠢的.不幸的是,当您添加参数时,我们将忽略该顺序,并且由于这是一个发布请求,因此要变通的代码并不简单.

When running the code below (with post) I didn't get the error you described, instead I got an error from Amazon indicating the key wasn't in the right order. Apparently Amazon relies on the order of the submitted fields which is damn stupid. Unfortunately we ignore that order when you add an argument and since this is a post request the code to workaround this is non-trivial.

我已经修复了Codename One的问题,该问题将遵循添加参数的自然顺序,但是由于我们已经发布了本周的版本,因此只能在下周五(2016年12月23日)进行.因此,上面的代码在更新后应该可以正常工作.

I've made a fix to Codename One that will respect the natural order in which arguments are added but since we already made this weeks release this will only go in next Friday (December 23rd 2016). So the code above should work as expected following that update.

较旧的答案供参考

我还没有测试过,但是类似的东西应该可以工作:

I haven't tested this but something like this should work:

private static final String S3_BUCKET_URL = "https://s3.amazonaws.com/my-bucket/";


public void uploadFileToS3(String filePath, String uniqueFileName) {
    MultipartRequest rq = new MultipartRequest(S3_BUCKET_URL);
    rq.addArgument("acl", "private");
    rq.addArgument("key", uniqueFileName);
    rq.addData("file", filePath, "image/jpeg");
    rq.setFilename("file", uniqueFileName);
    rq.setReadResponseForErrors(true);
    NetworkManager.getInstance().addToQueue(rq);
}

请注意,此操作不进行授权,因此假定存储桶至少可以访问以进行写入.

Notice that this doesn't do authorization so it assumes the bucket is accessible at least for write.

这篇关于代号一将相机拍摄的照片上传到亚马逊s3存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆