【问题标题】:How to return a file with Google Cloud Endpoints?如何使用 Google Cloud Endpoints 返回文件?
【发布时间】:2016-10-01 11:27:00
【问题描述】:

我有一种方法可以生成带有一些数据库记录的 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();
    }

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

【问题讨论】:

  • 嗨,如果您对可以(从端点)生成文件并将其保存到云存储并返回下载 URL 的解决方案感兴趣,请告诉我,我会详细回复。我已经做到了,而且效果很好。但我不确定这是你想要的。
  • 嗨@337186​​2 这对我来说可能是一个解决方案。但我希望这些文件有一个过期时间

标签: java google-app-engine google-cloud-endpoints google-cloud-platform


【解决方案1】:

以下是如何将文件从 Endpoint 保存到 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/cloud-console#_accessing

【讨论】:

  • PS:对于过期时间,您可以从 AppEngine 中,通过 Cron 作业,在到达过期时间时从 Cloud Storage 中删除文件
【解决方案2】:

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

Cloud Endpoints 实际上只是返回一个 JSON 字符串。您也可以尝试 Base64 将文件编码为字符串,然后在客户端中解码 Base64 字符串,但在我看来,从 CSV 文件开始执行我建议的操作可能更容易。您从 GAE 获得的响应也有 1MB 的限制。在此处阅读更多信息:Query response size limit on appengine?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 2015-09-07
    • 2016-05-18
    • 2018-12-18
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    相关资源
    最近更新 更多