这是将文件从端点保存到Cloud Storage并返回用于下载文件的URL的方法。
1 /在您的项目控制台中激活Google Cloud Storage
2 /在您的Cloud Storage实例中创建一个名称为bucketName的存储桶。可选:您可以在此存储桶上设置访问权限。
3 /在您的终结点类中,创建一个gcsService作为以下对象:
private final GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
.initialRetryDelayMillis(10)
.retryMaxAttempts(10)
.totalRetryPeriodMillis(15000)
.build());
4 /在您的方法中,创建一个ByteArrayOutputStream:
ByteArrayOutputStream os = new ByteArrayOutputStream();
5 /从ByteArrayOutputStream创建打印机
6 /然后执行以下操作:
ByteBuffer buf = ByteBuffer.wrap(os.toByteArray());
GcsFilename gcsfileName = new GcsFilename(bucketName, bucketFileName);
//bucketFileName = your file name
GcsFileOptions options = new GcsFileOptions.Builder().mimeType("text/plain").build();
GcsOutputChannel outputChannel = gcsService.createOrReplace(gcsfileName, options);
outputChannel.write(buf);
outputChannel.close();
7 /然后,您的文件应保存到Cloud Storage:您只需要在字符串包装器中返回URL即可打开它。查看以下文档,以确定要使用的URL(取决于是否对用户进行身份验证,请参阅“授予用户对对象的读取权限”部分)https://cloud.google.com/storage/docs/云控制台#_访问