How to upload media (image, video) to Google Cloud

2020-07-30 03:54发布

I am now facing with a problem once uploading media files to Google Cloud Storage by using Google Cloud Storage API Java. To be more specific, the GCS API Java example just help us upload text files to Google Bucket but is not useful for media files. I also see some discussion from ticket: Written file not showing up in Google Cloud Storage that the team suggest to use a gsutil tool written by Python. I am not using blobstore as well

My question is how can I do with the following requirements: -Creating and deleting buckets. -Uploading, downloading, and deleting objects (such as media files). -Listing buckets and objects. -Moving, copying, and renaming objects.

by implementing with Java?

I thank you very much for your time and look forward to hearing from you.

2条回答
贼婆χ
2楼-- · 2020-07-30 04:37
  • Upload/Download files: you can use the Blobstore API which can be configured to store blobs in Google Cloud Storage by specifying your bucket in the BlobstoreService createUploadUrl. Similarly, to download you can create a createGsBlobKey with the Bucket name + Object name which can then be served by the Blobstore service.
  • Create/Delete buckets: The Google Cloud Storage Java Client Library does not offers a way to create/delete buckets. You will need to use Google Cloud Storage REST API to programatically create and delete. Thought, you might want to consider organizing your data within one bucket.
  • Moving, copying, and renaming objects: make use of the Google Cloud Storage Java Client Library
查看更多
在下西门庆
3楼-- · 2020-07-30 04:50

I faced a similar requirement when I had to deal with all sorts of documents including media objects such as images and videos. This is the implementation I followed based on the official documentation of Google Cloud Examples project on GitHub: Source Link

To Upload a file

 public boolean uploadFile(String filePath, byte[] file) {
        try {
            setDefaultStorageCredentials();
            storage.create(BlobInfo.newBuilder(bucketName, filePath).build(),
                    new ByteArrayInputStream(file));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

To download a file

public byte[] downloadFile(String filePath) throws FileNotFoundException, IOException {
    setDefaultStorageCredentials();     
    return storage.get(bucketName).get(filePath).getContent();
}

To delete a file

public boolean deleteFile(String filePath){
    setDefaultStorageCredentials();                     
    return storage.delete(storage.get(bucketName).get(filePath).getBlobId());       
}

To provide temporary access to a file using a signed URL

public String getTemporaryFileLink(String filePath) throws Exception{
        setDefaultStorageCredentials();     
        Blob blob = storage.get(bucketName).get(filePath);      
        String blobName = blob.getName();       
        URL signedUrl = storage.signUrl(BlobInfo.newBuilder(bucketName, blobName).build(), 5,TimeUnit.MINUTES);
        return signedUrl.toExternalForm();
    }

Most of these methods are mentioned in this Google Github project. I just removed the clutter in my implementation. Hope this helps.

查看更多
登录 后发表回答