简体   繁体   中英

Upload File (image or video) from JSP to google cloud storage via Google app engine(JAVA)

I have Google App Engine (JAVA) server and Google Cloud Storage. In my server there is a JSP file with upload file form (the user pick file from his computer).

What I need to do is send the file to some servlet and then the servlet will upload the file to google cloud storage. I cannot upload the file directly from the JSP page. I need that this action will be from the servlet, in the server side. I tried so many thing that i dont know what to paste to here. I could upload a file to google cloud storage(GCS), but the GCS dont know what is the type of the file and cannot open it. I used the example here: https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-storage

How do I write the servlet and the jsp right ?

This is my code for file uploads.

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

As long as the filename includes the file extension, you should not face any issues. This is my implementation class:

import com.google.auth.Credentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.storage.Blob;

import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
* Uploads a file to Google Cloud Storage to the bucket specified in the
* BUCKET_NAME environment variable, appending a timestamp to end of the
* uploaded filename.
* 
* @throws IOException
* @throws FileNotFoundException
*/
@SuppressWarnings("static-access")
public class GoogleCloudStorage {

    Variables variables = new Variables();

    public GoogleCloudStorage() {       
        setDefaultStorageCredentials();
    }

    private static Storage storage = null;  
    private static Credentials credentials = null;
    //Project Id can be obtained from your GCP console dashboard.
    private static String projectId = "Your project Id";

    //Create the bucket using the REST API or manually using the Google Cloud Storage Browser Interface.
    private static String bucketName = "Your bucket name";

    //Following 4 parameters can be obtained from the Private Key file.
    //Client Id will usually be a numeric string
    private static String clientId = "Your Client Id From the Key File.";

    //Client Email Id is the email Id generated when you create the service account. This will be in the format of: *.iam.gserviceaccount.com
    private static String clientEmailId = "Your Client Email Id From the Key File.";

    //Private key can be obtained from the key file. This will be a very long string within the file. Paste the entire string here.
    private static String privateKey = "Your Private Key From the Key File.";

    //Private Key Id can be obtained from the key file. This is ususally a numeric string.
    private static String privateKeyId = "Your Client Id From the Key File.";

    /**
     * This method sets the storage credentials for the default storage object. 
     */
    private void setDefaultStorageCredentials() {
        try {           
            credentials = ServiceAccountCredentials.fromPkcs8(clientId, clientEmailId, privateKey, privateKeyId, null);         
            storage = StorageOptions.newBuilder()
                    .setCredentials(credentials)
                    .setProjectId(projectId).build().getService();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Uploads a given file to Google Cloud Storage.
     * 
     * @param filePath The desired file path for the file to be uploaded. File path should be absolute path and should include folders, sub-folders, and file name
     * @param file The file to be uploaded in byte array format
     * @return true if the file has been successfully uploaded; false otherwise
     */
    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;
        }
    }

    /**
     * Downloads a given file from Google Cloud Storage.
     * 
     * @param filePath The desired file path for the file to be downloaded. File path should be absolute path and should include folders, sub-folders, and file name
     * @return the downloaded file in byte array format
     */
    public byte[] downloadFile(String filePath) throws FileNotFoundException, IOException {
        setDefaultStorageCredentials();     
        return storage.get(bucketName).get(filePath).getContent();
    }

    /**
     * Generates a temporary link to a file in Google Cloud Storage. 
     * This will allow temporary access to the file without actually exposing the file.
     * Users accessing this link need not sign in using any credentials.
     * <p>
     * After the expiry time, this link will be expired and general public cannot access the file.
     * 
     * @param filePath The desired file path for the file to be uploaded. File path should be absolute path and should include folders, sub-folders, and file name
     * @return String containing the signed url for the file specified.
     */
    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();
    }

    /**
     * Deletes a given file from Google Cloud Storage.
     * 
     * @param filePath The desired file path for the file to be deleted. File path should be absolute path and should include folders, sub-folders, and file name
     * @return true if the file has been successfully deleted; false otherwise
     */
    public boolean deleteFile(String filePath){
        setDefaultStorageCredentials();                     
        return storage.delete(storage.get(bucketName).get(filePath).getBlobId());       
    }       

}

Reference: Google Cloud Storage Wrapper

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM