S3 upload

125 views
Skip to first unread message

Gareth Murfin

unread,
Jul 5, 2017, 6:53:24 AM7/5/17
to CodenameOne Discussions
I found some code that is supposed to work from stack over flow for s3 upload (shown below). However I always get this error when I try it

java.io.IOException: Error writing to server
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:666)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:678)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1534)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at com.codename1.impl.javase.JavaSEPort.getResponseCode(JavaSEPort.java:6248)
at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:403)
at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:282)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)


private void doUpload()
    {
        try {
            String strImage = Capture.capturePhoto();
            String s3Bucket = "https://usaeasygazy.s3.amazonaws.com";   //usa ones are easier no redirects?
            String fileName = "ARSE";
            MultipartRequest rq = new MultipartRequest();
            rq.setUrl(s3Bucket);
            rq.addArgumentNoEncoding("key", fileName);
            //rq.addArgument("acl", "public-read-write"); // here I give read-write access
            rq.addArgument("acl", "bucket-owner-full-control");
            rq.addData("file", strImage, "image/jpeg");
            
            _("add request to queue");
            NetworkManager.getInstance().addToQueue(rq);
            _("done");
        } catch (IOException ex) {
            _("ERROR DURING UPLOAD.! "+ex.getMessage());
        }
    }

Gareth Murfin

unread,
Jul 5, 2017, 8:09:31 AM7/5/17
to CodenameOne Discussions
I also tried Shais code from stack oveflow,

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);
        }
    }
});



this gives me the error 

java.io.IOException: Error writing to server
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:666)
at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:678)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1534)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
at com.codename1.impl.javase.JavaSEPort.getResponseCode(JavaSEPort.java:6248)
at com.codename1.io.ConnectionRequest.performOperation(ConnectionRequest.java:403)
at com.codename1.io.NetworkManager$NetworkThread.run(NetworkManager.java:282)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
java.lang.NullPointerException
at java.lang.String.<init>(String.java:566)
at userclasses.StateMachine.initVars(StateMachine.java:56)
at generated.StateMachineBase.startApp(StateMachineBase.java:54)
at generated.StateMachineBase.<init>(StateMachineBase.java:31)
at generated.StateMachineBase.<init>(StateMachineBase.java:96)
at userclasses.StateMachine.<init>(StateMachine.java:26)
at com.norby.uploader.NorbyS3.start(NorbyS3.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.codename1.impl.javase.Executor$1$1.run(Executor.java:106)
at com.codename1.ui.Display.processSerialCalls(Display.java:1151)
at com.codename1.ui.Display.mainEDTLoop(Display.java:968)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
Java Result: 1

Gareth Murfin

unread,
Jul 5, 2017, 9:02:29 AM7/5/17
to CodenameOne Discussions
just realised I have not authenticated to AWS in any way, thats probably the issue :) But I cant find any code snippets anywhere to actually do this ? Is there any AWS loggin code around I can see? Cheers, Gaz.

Gareth Murfin

unread,
Jul 5, 2017, 10:12:06 AM7/5/17
to CodenameOne Discussions


and now getting this error, still progress :-)) Im sure I read about this on stackvoerflow, the order thing, 

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>InvalidArgument</Code><Message>Bucket POST must contain a field named 'key'.  If it is specified, please check the order of the fields.</Message><ArgumentName>key</ArgumentName><ArgumentValue></ArgumentValue><RequestId>B8AF949922CB80EF</RequestId><HostId>Qws3NOeXsWpdBqZYTzw22b8glWfAPtlL986Auy2UfiZRk0Xul8NUP0626IiH5V+hACgHrUWkP94=</HostId></Error>

Gareth Murfin

unread,
Jul 5, 2017, 10:26:25 AM7/5/17
to CodenameOne Discussions
Weirdly I updated the project libs and they came down (when you start a new project does it use old libs or something?) and the error has now changed to:

Error response: <?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>8D202DD01F08C7AD</RequestId><HostId>OTi61ymlaENhFrgxAPCw76S6DcyNCorNr+RP6VAAP11VUplbRoBGIuK8WvrL8VSnVrqKlNfV94o=</HostId></Error>

--
You received this message because you are subscribed to a topic in the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/codenameone-discussions/pEPrG2d4IQw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/769d0d0c-2f76-4dc0-b708-b8e963a74127%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Gareth Murfin
(Android Freelancer - www.garethmurfin.co.uk)

Gareth Murfin

unread,
Jul 5, 2017, 10:33:21 AM7/5/17
to CodenameOne Discussions
OK fixed, my permissions in the AWS console for the bucket were not set to allow everyone to read and write, not sure how dangerous that is (or how you access with existing details and authenticate) but it does upload now!.. nice one ;)

To unsubscribe from this group and all its topics, send an email to codenameone-discussions+unsubscr...@googlegroups.com.
--
Gareth Murfin
(Android Freelancer - www.garethmurfin.co.uk)

Shai Almog

unread,
Jul 6, 2017, 1:04:24 AM7/6/17
to CodenameOne Discussions
When you create a new project it will provide the libs we had when we created the plugin

Gareth Murfin

unread,
Jul 6, 2017, 9:18:50 AM7/6/17
to CodenameOne Discussions
Ah that makes sense then, I saw that I needed to update to get aroudn the order bug for supply post params, works nice. Thanks Shai.
Reply all
Reply to author
Forward
0 new messages