您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用Google Cloud Endpoints返回文件?

如何使用Google Cloud Endpoints返回文件?

这是将文件从端点保存到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/云控制台#_访问

Go 2022/1/1 18:29:03 有311人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶