如何使用GoogleServiceAuthentication类将文件上传到grails中的google存储?

如何使用GoogleServiceAuthentication类将文件上传到grails中的google存储?,grails,groovy,google-cloud-platform,google-cloud-storage,Grails,Groovy,Google Cloud Platform,Google Cloud Storage,我正在制作一个应用程序,在其中我需要上传Excel文件到谷歌存储 有人能告诉我如何使用GoogleServiceAuthentication在Google存储中上传文件吗 我正试图使用操作uploadExcelFile在FileUploadController中上载excelFile 代码: def uploadExcelFile(){ def excelFile = request.getFile('excelFile') // UPLOAD HERE redi

我正在制作一个应用程序,在其中我需要上传Excel文件到谷歌存储

有人能告诉我如何使用GoogleServiceAuthentication在Google存储中上传文件吗

我正试图使用操作
uploadExcelFile
FileUploadController
中上载
excelFile

代码:

def uploadExcelFile(){

    def excelFile = request.getFile('excelFile')

    // UPLOAD HERE 

    redirect(action: "index")
}

您需要在IAM下设置一个服务帐户,并授予该帐户访问您的存储桶的权限。创建帐户后,您可以下载json格式的私钥。然后,您可以在代码中使用该键:

StorageOptions options = StorageOptions.newBuilder()
    .setCredentials(GoogleCredentials.fromStream(new ByteArrayInputStream("YOUR KEY FROM JSON FILE".getBytes())))
    .build();

Storage storage = options.getService();
拥有存储对象后,您可以检索存储桶并将文件上载到其中:

Bucket bucket = storage.get("your-bucket-name")

try {
    bucket.create("name of file", excelFile.inputStream)
}
catch (Exception e) {//do something}