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

时间:2022-12-17 23:10:45

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

我有一个生成带有一些db记录的CSV文件的方法

    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?

我希望该方法是一个端点,以便从任何类型的客户端(Web或移动设备)调用它来下载它。在Google Cloud Endpoint文档中,没有关于File作为有效返回类型的内容。我怎样才能创建返回文件的端点?

2 个解决方案

#1


3  

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

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

1/ Activate Google Cloud Storage in your Project Console

1 /在项目控制台中激活Google云端存储

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

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

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

3 /在端点类中,创建一个gcsService作为folllow:

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

4/ In your method, create an ByteArrayOutputStream:

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

ByteArrayOutputStream os = new ByteArrayOutputStream();

5/ Create your printer from the ByteArrayOutputStream

5 /从ByteArrayOutputStream创建打印机

6/ Then do the following:

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/ 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

7 /您的文件应该保存到云存储:您只需要在字符串包装器中返回要打开它的URL。查看以下文档以确定要使用的URL(取决于是否应对用户进行身份验证,请参阅“授予用户对对象的读取权限”一节)https://cloud.google.com/storage/docs/云控制台#_accessing

#2


0  

CSV files are pretty easy to work with once back in a client - after all, all they are is a specially formatted String with the commas and quotes and what not. What you could do is just return a String in your endpoints method - the entire String being your CSV file. Then in the client you know that String is the CSV so handle it accordingly (i.e. write it to a file and save it with the .csv extension).

CSV文件很容易在客户端中使用 - 毕竟,它们只是一个带有逗号和引号的特殊格式的字符串,而不是。你可以做的只是在你的端点方法中返回一个字符串 - 整个字符串是你的CSV文件。然后在客户端中,您知道String是CSV,因此请相应地处理它(即将其写入文件并使用.csv扩展名保存)。

Cloud Endpoints really just return a JSON String. You could also try Base64 encoding a File to String and then decoding the Base64 String in your client but in my opinion it might be easier to just do what I suggested since CSV files. There is also a 1MB limit on the response you get from GAE. Read more about it here: Query response size limit on appengine?

Cloud Endpoints实际上只返回一个JSON字符串。你也可以尝试Base64编码一个文件到字符串,然后解码你的客户端的Base64字符串,但在我看来,可能更容易做我自CSV文件以来的建议。您从GAE获得的响应也有1MB的限制。在这里阅读更多相关信息:查询appengine的响应大小限制?

#1


3  

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

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

1/ Activate Google Cloud Storage in your Project Console

1 /在项目控制台中激活Google云端存储

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

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

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

3 /在端点类中,创建一个gcsService作为folllow:

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

4/ In your method, create an ByteArrayOutputStream:

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

ByteArrayOutputStream os = new ByteArrayOutputStream();

5/ Create your printer from the ByteArrayOutputStream

5 /从ByteArrayOutputStream创建打印机

6/ Then do the following:

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/ 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

7 /您的文件应该保存到云存储:您只需要在字符串包装器中返回要打开它的URL。查看以下文档以确定要使用的URL(取决于是否应对用户进行身份验证,请参阅“授予用户对对象的读取权限”一节)https://cloud.google.com/storage/docs/云控制台#_accessing

#2


0  

CSV files are pretty easy to work with once back in a client - after all, all they are is a specially formatted String with the commas and quotes and what not. What you could do is just return a String in your endpoints method - the entire String being your CSV file. Then in the client you know that String is the CSV so handle it accordingly (i.e. write it to a file and save it with the .csv extension).

CSV文件很容易在客户端中使用 - 毕竟,它们只是一个带有逗号和引号的特殊格式的字符串,而不是。你可以做的只是在你的端点方法中返回一个字符串 - 整个字符串是你的CSV文件。然后在客户端中,您知道String是CSV,因此请相应地处理它(即将其写入文件并使用.csv扩展名保存)。

Cloud Endpoints really just return a JSON String. You could also try Base64 encoding a File to String and then decoding the Base64 String in your client but in my opinion it might be easier to just do what I suggested since CSV files. There is also a 1MB limit on the response you get from GAE. Read more about it here: Query response size limit on appengine?

Cloud Endpoints实际上只返回一个JSON字符串。你也可以尝试Base64编码一个文件到字符串,然后解码你的客户端的Base64字符串,但在我看来,可能更容易做我自CSV文件以来的建议。您从GAE获得的响应也有1MB的限制。在这里阅读更多相关信息:查询appengine的响应大小限制?