• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Fail to upload photo in AmazonS3

 
Ranch Hand
Posts: 37
C++ Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I was trying to upload the photo into Amazon-S3 of my own bucket and retrieve from there in order to display on my webpage.
For the retrieve the object URLand display on my webpage is work fine, if I manually upload the photo into my bucket.
Whenever I am upload a photo into my bucket, it is success without any error, but when the time i trying to view the photo from my bucket, it is a broken/damaged photo.
Thanks for advance.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, welcome to CodeRanch!

Since we don't know at this point if the problem is in processing of multi-part byte stream or uploading to S3, I'd suggest first commenting out the S3 uploading and dumping 'dataBytes' into a new local file.
If that local file is also broken, then you know the problem is in byte stream processing, not in S3 uploading.
 
gah hon
Ranch Hand
Posts: 37
C++ Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:Hi, welcome to CodeRanch!

Since we don't know at this point if the problem is in processing of multi-part byte stream or uploading to S3, I'd suggest first commenting out the S3 uploading and dumping 'dataBytes' into a new local file.
If that local file is also broken, then you know the problem is in byte stream processing, not in S3 uploading.



I had tried what you said, copy the image into local storage. And the result is no broken.

Therefore, I could say that is a problem with the S3 Upload by based on your sentence.
Thanks for advance.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, it seems to me that the byte streams in these 2 snippets are not the same.

In the 1st snippet with S3 uploading, you are reading *everything* in the form data (including all the multipart metadata such as content type strings,
boundary strings, etc) into dataBytes and uploading all of it to S3, not just the image data.


However, in the 2nd snippet with local file system, you are correctly positioning startPos past all the multipart metadata at start of image data and writing only the image data to file.


You should be doing the same in the S3 case. The data to S3 should have only the image data, without all the multipart metadata.

Most importantly, a better approach is to use the Apache FileUpload framework to handle all of this. Manually processing multipart data like this is neither a good idea nor does it make for elegant readable code.
 
gah hon
Ranch Hand
Posts: 37
C++ Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:First of all, it seems to me that the byte streams in these 2 snippets are not the same.

In the 1st snippet with S3 uploading, you are reading *everything* in the form data (including all the multipart metadata such as content type strings,
boundary strings, etc) into dataBytes and uploading all of it to S3, not just the image data.


However, in the 2nd snippet with local file system, you are correctly positioning startPos past all the multipart metadata at start of image data and writing only the image data to file.


You should be doing the same in the S3 case. The data to S3 should have only the image data, without all the multipart metadata.

Most importantly, a better approach is to use the Apache FileUpload framework to handle all of this. Manually processing multipart data like this is neither a good idea nor does it make for elegant readable code.



I am understand your statement. But I have no clue how to achieve that even though I did research.
Please send me example. Thanks for advance.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No clue? Their user guide https://commons.apache.org/proper/commons-fileupload/using.html seems clear enough...
 
gah hon
Ranch Hand
Posts: 37
C++ Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:No clue? Their user guide https://commons.apache.org/proper/commons-fileupload/using.html seems clear enough...



I had made modification on the code, but the image is still broken whereas the file size is exactly corrected.

 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You are creating an empty byte array of size (endPos - startPos) and uploading it. It'll just be an array of zeros. Of course it's broken!

Why are you not changing the code to use Apache FileUpload?
 
gah hon
Ranch Hand
Posts: 37
C++ Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:
You are creating an empty byte array of size (endPos - startPos) and uploading it. It'll just be an array of zeros. Of course it's broken!

Why are you not changing the code to use Apache FileUpload?



It is the assignment requirement to use to byte array.
However, how could I capture the only image file and convert it into byte array?
I kind of confuse that part.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have already done something very similar in the local file snippet.
Combine that with http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html#ByteArrayInputStream(byte[],%20int,%20int)
 
gah hon
Ranch Hand
Posts: 37
C++ Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthik Shiraly wrote:You have already done something very similar in the local file snippet.
Combine that with http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html#ByteArrayInputStream(byte[],%20int,%20int)




Yeah, The problem is solved by kindly the constructor into 3 parameters as what I did to the local file.
Thanks a lot.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gah hon wrote:

Karthik Shiraly wrote:You have already done something very similar in the local file snippet.
Combine that with http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html#ByteArrayInputStream(byte[],%20int,%20int)




Yeah, The problem is solved by kindly the constructor into 3 parameters as what I did to the local file.
Thanks a lot.



Can I have the solution of ByteArrayInputStream? gah hon.
Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic