如何 return 具有 Google 云端点的文件?
How to return a file with Google Cloud Endpoints?
我有一个方法可以生成包含一些数据库记录的 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();
}
我希望该方法是一个端点,以便从任何类型的客户端(Web 或移动设备)调用它来下载它。在 Google Cloud Endpoint documentation 中,没有关于 File 的内容作为有效的 return 类型。我如何创建和终止 return 文件?
CSV 文件一旦回到客户端就很容易使用 - 毕竟,它们只是一个特殊格式的字符串,带有逗号和引号等等。您可以做的只是 return 端点方法中的一个字符串——整个字符串就是您的 CSV 文件。然后在客户端中,您知道 String 是 CSV,因此相应地处理它(即将它写入文件并使用 .csv 扩展名保存)。
Cloud Endpoints 实际上只是 return 一个 JSON 字符串。您也可以尝试使用 Base64 将文件编码为字符串,然后在您的客户端中解码 Base64 字符串,但在我看来,从 CSV 文件开始,按照我的建议进行操作可能更容易。您从 GAE 获得的响应也有 1MB 的限制。在这里阅读更多相关信息:Query response size limit on appengine?
以下是如何将文件从 Endpoint 保存到 Cloud Storage 以及 return URL 下载文件的方法。
1/ 在您的项目控制台中激活 Google 云存储
2/ 在您的云存储实例中创建一个名为 bucketName 的存储桶。可选:您可以在此存储桶上设置访问权限。
3/ 在您的端点 Class 中,创建一个 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/ 然后您的文件应该保存到云存储:您只需 return 在字符串包装器中 url 即可打开它。查看以下文档来决定使用哪个 URL(取决于用户是否需要经过身份验证,请参阅第 "A user is granted read access to an object" 节)https://cloud.google.com/storage/docs/cloud-console#_accessing
我有一个方法可以生成包含一些数据库记录的 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();
}
我希望该方法是一个端点,以便从任何类型的客户端(Web 或移动设备)调用它来下载它。在 Google Cloud Endpoint documentation 中,没有关于 File 的内容作为有效的 return 类型。我如何创建和终止 return 文件?
CSV 文件一旦回到客户端就很容易使用 - 毕竟,它们只是一个特殊格式的字符串,带有逗号和引号等等。您可以做的只是 return 端点方法中的一个字符串——整个字符串就是您的 CSV 文件。然后在客户端中,您知道 String 是 CSV,因此相应地处理它(即将它写入文件并使用 .csv 扩展名保存)。
Cloud Endpoints 实际上只是 return 一个 JSON 字符串。您也可以尝试使用 Base64 将文件编码为字符串,然后在您的客户端中解码 Base64 字符串,但在我看来,从 CSV 文件开始,按照我的建议进行操作可能更容易。您从 GAE 获得的响应也有 1MB 的限制。在这里阅读更多相关信息:Query response size limit on appengine?
以下是如何将文件从 Endpoint 保存到 Cloud Storage 以及 return URL 下载文件的方法。
1/ 在您的项目控制台中激活 Google 云存储
2/ 在您的云存储实例中创建一个名为 bucketName 的存储桶。可选:您可以在此存储桶上设置访问权限。
3/ 在您的端点 Class 中,创建一个 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/ 然后您的文件应该保存到云存储:您只需 return 在字符串包装器中 url 即可打开它。查看以下文档来决定使用哪个 URL(取决于用户是否需要经过身份验证,请参阅第 "A user is granted read access to an object" 节)https://cloud.google.com/storage/docs/cloud-console#_accessing