如何使用Google Cloud Endpoints返回文件? [英] How to return a file with Google Cloud Endpoints?

查看:108
本文介绍了如何使用Google Cloud Endpoints返回文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有生成CSV一些分贝记录文件的方法



 公共静态无效generateCsvForAttendees(列表与LT;与会者> attendeeList )抛出FileNotFoundException 
{
PrintWriter pw = new PrintWriter(new File(test.csv));

StringBuilder sb = new StringBuilder();

//标题
sb.append(Id);
sb.append(',');
sb.append(Name);
sb.append(',');
sb.append(Lastname);
sb.append('\\\
');

//内容
(与会者与会者:attendeeList)
{
sb.append(attendee.getId());
sb.append(',');
sb.append(attendee.getUser()。getName());
sb.append(',');
sb.append(attendee.getUser()。getLastname());
sb.append('\\\
');



pw.write(sb.toString());
pw.close();
}

我希望这个方法可以成为一个端点,一种客户端(网络或手机)下载它。在谷歌云端点文档有是不是关于文件作为有效的返回类型。我如何创建和返回文件的端点?

解决方案

以下是如何将文件从端点保存到云存储并返回用于下载的URL。



1 /在您的项目控制台中激活Google Cloud Storage



在您的Cloud Storage实例中创建一个存储桶, bucketName。可选:您可以在此存储桶上设置访问权限。

3在您的端点类中,按照以下方式创建gcsService:

'pre> 私人最终GcsService gcsService = GcsServiceFactory.createGcsService(新RetryParams.Builder()
.initialRetryDelayMillis(10)
.retryMaxAttempts(10)
.totalRetryPeriodMillis(15000)
.build());

4 /在您的方法中,创建一个ByteArrayOutputStream:

  ByteArrayOutputStream os = new ByteArrayOutputStream(); 

5从ByteArrayOutputStream创建打印机

< p / 6>然后执行以下操作:

pre $ ByteBuffer buf = ByteBuffer.wrap(os.toByteArray());
GcsFilename gcsfileName = new GcsFilename(bucketName,bucketFileName);
// bucketFileName =您的文件名
GcsFileOptions options = new GcsFileOptions.Builder()。mimeType(text / plain)。build();
GcsOutputChannel outputChannel = gcsService.createOrReplace(gcsfileName,options);
outputChannel.write(buf);
outputChannel.close();

7 /您的文件应保存到云存储中:您只需返回字符串包装打开它的网址。请查看以下文档以确定要使用哪个URL(具体取决于用户是否需要进行身份验证,请参见用户被授予对对象的读取权限一节) https://cloud.google.com/storage/docs/cloud-console#_accessing


I have a method that generate a CSV file with some db records

    public static void generateCsvForAttendees( List<Attendee> attendeeList ) throws FileNotFoundException
    {
        PrintWriter pw = new PrintWriter(new File("test.csv"));

        StringBuilder sb = new StringBuilder();

        //Header
        sb.append( "Id" );
        sb.append( ',' );
        sb.append( "Name" );
        sb.append( ',' );
        sb.append( "Lastname" );
        sb.append('\n');

        //Content
        for( Attendee attendee: attendeeList )
        {
            sb.append( attendee.getId() );
            sb.append( ',' );
            sb.append( attendee.getUser().getName() );
            sb.append( ',' );
            sb.append( attendee.getUser().getLastname() );
            sb.append( '\n' );

        }

        pw.write(sb.toString());
        pw.close();
    }

I would like that method would be an endpoint in order to invoke it from any kind of client (web or mobile) to download it. In the Google Cloud Endpoint documentation there isn't something about File as a valid return type. How could I create and endpoint that return a File?

解决方案

Here is how to save a file from an Endpoint to Cloud Storage and return the URL for downloading it.

1/ Activate Google Cloud Storage in your Project Console

2/ Create a Bucket in your Cloud Storage instance, with the name bucketName. Optional: you can set access rights on this bucket.

3/ In your endpoint Class, create a gcsService as folllow:

private final GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
        .initialRetryDelayMillis(10)
        .retryMaxAttempts(10)
        .totalRetryPeriodMillis(15000)
        .build());

4/ In your method, create an ByteArrayOutputStream:

ByteArrayOutputStream os = new ByteArrayOutputStream();

5/ Create your printer from the ByteArrayOutputStream

6/ Then do the following:

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/ Your file should then be saved to Cloud Storage: you just have to return in a string wrapper the url to open it. Look the following documentation to decide which URL to use (depending on whether the user shall be authenticated or not, see Section "A user is granted read access to an object") https://cloud.google.com/storage/docs/cloud-console#_accessing

这篇关于如何使用Google Cloud Endpoints返回文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆