So DigitalOcean is great. It’s one of the best cloud platforms and it’s very affordable, that is why I chose it as a backbone for most of my project. Don’t get me wrong – I’m not saying Amazon is worse than Digital Ocean. There is pros and cons to each of the two. DigitalOcean is easier to use, but less flexible. AWS has a steeper learning curve, but you can get more out of it. It’s up to you what you choose.
DigitalOcean’s API
When it comes to Spaces (Buckets), DigitalOcean chose approach which is advantageous for everyone. Instead of re-inventing the wheel by creating their own API framework for Bucket and Object manipulation, they implemented Amazon’s SDK. What this means is that you can use existing Amazon AWS SDK – which you already might have experience with – when implementing a DigitalOcean-dependent solution. I will provide you with code implementation later in this article.
Image vs Base64 string
When it comes to Java, image processing can be a pain in the ass, especially if you use OpenJDK 8 or above. OpenJDK does not include any image processing features, so you will have to troubleshoot your app until you find a way to make it work.
But generally, you have two options:
- Locate image on your disk and create a File out of it. Then, use this file as a parameter for AWS SDK method putObject()
- Locate image on your disk and convert it into base64-encoded byte array. Then, load this array into InputString and use that for AWS SDK method putObject()

I will explain the second option in this article as it’s a bit more complicated than the first one.
Send image to DigitalOcean Bucket
Imagine that you have a web service which is supposed to receive an image in the incoming request. You can send this image from client as an object with MIME type “image/jpeg” (or similar) using a multipart/form-data request, or you can send it as a Base64-encoded byte array representation of an image, as a parameter of your request body.
I prefer the second option. It’s less complicated to implement and work with. So you have a client that sends this string to your web service. You need to get this string, base64-decode it, store it as a byte array and load it into InputStream. While doing that, you need to figure where exactly you will store your file on Bucket. It’s because you might want to return it’s URL to your client, so it can parse the image.
public String uploadBase64ToStorage(int userid, String location, String strimage) { AWSCredentialsProvider awscp = new AWSStaticCredentialsProvider( new BasicAWSCredentials("DO_ACCESS_KEY", "DO_SECRET_KEY") ); AmazonS3 space = AmazonS3ClientBuilder .standard() .withCredentials(awscp) .withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration("BUCKET_ENDPOINT", "BUCKET_REGION") ) .build(); byte[] byteimage = Base64.getDecoder().decode(strimage); InputStream is = new ByteArrayInputStream(byteimage); ObjectMetadata om = new ObjectMetadata(); om.setContentLength(byteimage.length); om.setContentType("image/jpg"); String filepath = /somefolder/someanotherfolder/testfile.jpg"; space.putObject("BUCKET_NAME", filepath, is, om); return space.getUrl("BUCKET_NAME", filepath).toString(); }
So to break it down:
- You receive Base64-encoded byte array representation of the image
- You Base64-decode it and store it in byte[]
- You initialize AWSCredentialsProvider used for authentication.
- You initialize your bucket using AmazonS3 object
- You create a ObjectMetadata which will hold information about object type, in our case image/jpg
- You decide where the file should be stored, and save this path in a String variable
- You send the object using space.putObject() method
- You return the public URL for the image to the client using space.getUrl() method
And that’s all. Fairly simple.
Let me know if this article helped you. Leave a comment below this article. Good luck!