Posted on 6 Comments

Accessing Media Files (Audio, Video, Images…) From DigitalOcean Spaces Using Java

Accessing Media Files (Audio, Video, Images...) From DigitalOcean Spaces Using Java 1

Contents

3.4
(5)

Recently, I need to upgrade my English learning app on Android to use multiple servers. Previously, I used two servers already. However, the way I manage media files caused me a lot of headache. So, I decided to switch to DigitalOcean since they have servers around the world and the pricing is good ($5 for 250GB storage and 1TB monthly transfer, which is good).

The initial problem with DigitalOcean spaces

Coming from old-school servers, I thought I can upload the files to DigitalOcean spaces using FTP and access the files directly as I usually used. However, the first problem is you cannot upload to spaces using FTP and the second problem is in order to make a file accessible to public, you need to set it permission to public. If you have one or two files, that wouldn’t be a problem.

I have more than 18,000 files.

The solutions to uploading multiple files

It turned out that DigitalOcean spaces use Amazon s3 technologies. So, in order to upload to spaces, you need to have FileZilla Pro (paid) or CyberDuck(free). I chose FileZilla Pro because it allows you to resume failed transfers while CyberDuck doesn’t allow that (or I couldn’t find where is that option).

The solution to accessing files

As my app used by thousands of people, the media files need to be accessible to all of the users. Actually, it is very easy to generate a public accessible URL for any files (they are called objects) using the aws-android-sdk. In case you need a working sample code, here it is:

public class DigitalOcean implements MediaServer {
    private static final String KEY = "YOUR_KEY";
    private static final String SECRETS = "YOUR_SECRETS";
    private String endpoint, bucketName;
    private AmazonS3Client s3Client;

    @Override
    public String getEndpoint() {
        return endpoint;
    }

    @Override
    public String getType() {
        return "s3";
    }

    public DigitalOcean(String endpoint, String bucketName) {
        this.bucketName = bucketName;
        this.endpoint = endpoint;
        AWSCredentials myCredentials = new BasicAWSCredentials(KEY, SECRETS);
        s3Client = new AmazonS3Client(myCredentials);
        s3Client.setEndpoint(this.endpoint);
    }

    public String getMediaURL(String objectPath) {
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectPath);
        URL objectURL = s3Client.generatePresignedUrl(request);
        return objectURL.toString();
    }
}
 

As you can see, I created an object to generate URL to any files(objects). For example, here is a sample space url:

Accessing Media Files (Audio, Video, Images...) From DigitalOcean Spaces Using Java 2

The bucket name would be data-sample.

If your file, let call it song.mp3 located at /data-sample/media/song.mp3, the objectPath would be: /media/song.mp3.

If you run the method getMediaURL(), you will get the path to the file that is accessible to your users but you don’t have to mark the file public.

https://data-sample.sgp1.digitaloceanspaces.com/media/song.mp3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20180925T060309Z&X-Amz-SignedHeaders=host&X-Amz-Expires=899&X-Amz-Credential=BP5K25LRRQWYV2CMHZLL%2F20180925%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=6a21133ced60047cfba60053afcdf712997c474585ade2f0050bc853b0a4db89

Click on a star to rate it!

Average rating 3.4 / 5. Vote count: 5

No votes so far! Be the first to rate this post.

6 thoughts on “Accessing Media Files (Audio, Video, Images…) From DigitalOcean Spaces Using Java

  1. You save my life! Thank you!

    1. You’re welcome, bro 🙂

      1. Thanks for writing this interesting article and you surely saved the lifes of so many people by sharing this great content. However, I just faces one issue. You see I don’t own the typical DigitalOcean web server and instead I own the managed DO server which is being managed by Cloudways. You can see the details of the article for yourself by clicking below: cloudways.com/en/digital-ocean-cloud-hosting.php
        So, if you could refer me a way around then it will be highly appreciated.

        1. Hi @Muhammad,

          I’m sorry for the late reply.

          I don’t have experience with Cloudways. I think if they use DO, the mechanic would be the same. If you want, please exchange further details over my emails: dat.tm24[@gmail.com] (please remove the [ and ])

  2. Hello.

    There is not scripts mentionned into your example.

    How can you invoke BasicAWSCredentials, AmazonS3Client,GeneratePresignedUrlRequest, etc. ?

    I mean, to use your code, what javascript API files do we need to include ?

    Thanks.

    1. Hi,

      The code is for Java, not javascript. You may want to search for documentation for JS for more details.

Leave a Reply

Your email address will not be published. Required fields are marked *