简体   繁体   English

如何在本地开发服务器上启用 Google Cloud Storage?

[英]How do I enable Google Cloud Storage on my local development server?

I want to use a GCS bucket as the backing for my blobstore but I cannot figure out how to set one up on my development server.我想使用 GCS 存储桶作为我的 blobstore 的支持,但我不知道如何在我的开发服务器上设置一个。

There are instructions for doing this using the developers console on the live server, but I can't find anything about how to do it on my local development machine...有使用实时服务器上的开发人员控制台执行此操作的说明,但我找不到有关如何在本地开发机器上执行此操作的任何说明...

Turns out you don't need to perform any setup at all.事实证明,您根本不需要执行任何设置。 I just assumed there was one with particular name when uploading using the blobstore and one was created for me automatically.我只是假设在使用 blobstore 上传时有一个具有特定名称的,并且一个是自动为我创建的。

Incidentally, it does not seem to be documented anywhere how you can browse files in the storage of the development server.顺便说一句,似乎没有任何地方记录如何浏览开发服务器存储中的文件。 You can do it by selecting the __GsFileInfo__ entity in the Datastore Viewer admin access to your local dev server.您可以通过在 Datastore Viewer 管理员访问中选择__GsFileInfo__实体来访问您的本地开发服务器。

For those trying to get Google Cloud Storage to work at all from their local Java development app server, I thought one more answer would be helpful.对于那些试图通过本地 Java 开发应用服务器让 Google Cloud Storage 正常工作的人,我认为还有一个答案会有所帮助。 I managed to get my local dev app server working with non-local Google Cloud Storage, but only be digging through the code and figuring out what was needed - there doesn't appear to be documentation on this.我设法让我的本地开发应用服务器与非本地 Google Cloud Storage 一起工作,但只是在挖掘代码并弄清楚需要什么 - 似乎没有关于此的文档。

The goal will be to get this block of code to work locally, which reads a file from GCS:目标是让这段代码在本地工作,它从 GCS 读取文件:

    GcsService gcsService =
       GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
    int fileSize = (int) gcsService.getMetadata(gcsFilename).getLength();
    ByteBuffer byteBuffer = ByteBuffer.allocate(fileSize);
    GcsInputChannel inputChannel = gcsService.openReadChannel(gcsFilename, 0);
    int readResult = inputChannel.read(byteBuffer);
    byte[] fileBytes = byteBuffer.array();

If you try to do this locally, you won't find any files you've upload to GCS, because it will be trying to use a fake local GCS.如果您尝试在本地执行此操作,您将找不到上传到 GCS 的任何文件,因为它会尝试使用伪造的本地 GCS。 Unfortunately, I haven't found a good way to upload to this local GCS, so that isn't very useful (there is no file explorer for it as there is in the cloud version, and gsutil doesn't work for it).不幸的是,我还没有找到上传到这个本地 GCS 的好方法,所以这不是很有用(没有云版本中的文件浏览器,并且 gsutil 不适用于它)。 So instead, we're going to get it to work with non-local (cloud) GCS when running in the local dev app server.因此,当在本地开发应用服务器中运行时,我们将使其与非本地(云)GCS 一起使用。

To do that, note that GcsService is created in com.google.appengine.tools.cloudstorage.GcsServiceFactory by this block of code:为此,请注意 GcsService 是通过以下代码块在 com.google.appengine.tools.cloudstorage.GcsServiceFactory 中创建的:

if (location == SystemProperty.Environment.Value.Production || hasCustomAccessTokenProvider()) {
  rawGcsService = OauthRawGcsServiceFactory.createOauthRawGcsService(builder.build());
} else if (location == SystemProperty.Environment.Value.Development) {
  rawGcsService = LocalRawGcsServiceFactory.createLocalRawGcsService();

The above says that you need to specify a custom access token provider to get the non-local service, which you do by defining a system property.上面说您需要指定一个自定义访问令牌提供程序来获取非本地服务,您可以通过定义系统属性来实现。 For an app engine app, you can do that in appengine-web.xml like this:对于应用引擎应用,您可以在 appengine-web.xml 中执行此操作,如下所示:

<system-properties>
    <property name="gcs_access_token_provider" value="com.mypackage.MyAccessTokenProvider" />
</system-properties>

This value of that property is a class you define that implements com.google.appengine.tools.cloudstorage.oauth.AccessTokenProvider, which provides the access token for your app.该属性的此值是您定义的一个类,用于实现 com.google.appengine.tools.cloudstorage.oauth.AccessTokenProvider,它为您的应用程序提供访问令牌。 That class needs to create a GoogleCredential, which can be used to fetch an access token, using the instructions for "Other" for a GoogleCredential on https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests .该类需要创建一个可用于获取访问令牌的 GoogleCredential,使用https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests 上GoogleCredential 的“其他”说明。

Now it will create an OAuth GcsService that talks to the cloud, and you don't need to use the fake local storage.现在它将创建一个与云通信的 OAuth GcsService,您不需要使用伪造的本地存储。

You need to download and integrate the Google Cloud Storage Client Library for App Engine.您需要下载并集成适用于 App Engine 的Google Cloud Storage 客户端库

This library provides you the GcsService which is similar to the BlobstoreService , so you can write a file, read a file, delete a file and other functions provided from Cloud Storage该库为您提供了类似于BlobstoreServiceGcsService ,因此您可以编写文件、读取文件、删除文件以及Cloud Storage提供的其他功能

When you use the code in the development environment, the uploaded files are stored in the appengine-generated folder and an __GsFileInfo__ entity is created in the local datastore which preserves the metadata attached to the file在开发环境中使用代码时,上传的文件存储在appengine-generated文件夹中,并在本地数据存储中创建一个__GsFileInfo__实体,该实体保留附加到文件的元数据

This library works online too, so your code will work for both development and production environments.该库也可在线运行,因此您的代码适用于开发和生产环境。

Here you can find the Getting Started guide and the full JavaDoc reference在这里您可以找到入门指南和完整的JavaDoc参考

Figured this could use some more recent findings.认为这可以使用一些最近的发现。 For me, the local cloud storage doesn't work at all.对我来说,本地云存储根本不起作用。 It fails with an error saying GsFileInfo is a protected entity.它失败并显示GsFileInfo是受保护实体的错误。 It works fine if you don't fire up the local datastore emulator (meaning it uses the remote cloud storage).如果您不启动本地数据存储模拟器(意味着它使用远程云存储),它可以正常工作。

This seems to jive with google's latest docs: https://cloud.google.com/appengine/docs/standard/java/using-cloud-storage#using_cloud_storage_with_the_local_development_server这似乎与谷歌的最新文档一致: https : //cloud.google.com/appengine/docs/standard/java/using-cloud-storage#using_cloud_storage_with_the_local_development_server

The App Engine local development server doesn't emulate Cloud Storage, so all Cloud Storage requests must be sent over the Internet to an actual Cloud Storage bucket. App Engine 本地开发服务器不会模拟 Cloud Storage,因此所有 Cloud Storage 请求都必须通过互联网发送到实际的 Cloud Storage 存储桶。

Thus the answer from @learnj12 is the best approach.因此,@learnj12 的答案是最好的方法。 However the GoogleCredential has been deprecated, and it's unclear how to implement com.google.appengine.tools.cloudstorage.oauth.AccessTokenProvider.但是 GoogleCredential 已被弃用,目前尚不清楚如何实现 com.google.appengine.tools.cloudstorage.oauth.AccessTokenProvider。

Here's what worked for me:以下是对我有用的内容:

import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
import com.google.appengine.tools.cloudstorage.oauth.AccessTokenProvider;

public class GCSTokenProvider implements AccessTokenProvider {
  @Override
  public AppIdentityService.GetAccessTokenResult getNewAccessToken(List<String> list) {
    return AppIdentityServiceFactory.getAppIdentityService().getAccessToken(Collections.singleton("cloud-platform"));
  }
}

NB.注意。 I'm using a service account, and am logged in with gcloud .我正在使用服务帐户,并使用gcloud登录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 本地开发服务器上的Google云端存储:文件名无效 - Google Cloud Storage on local development Server: Invalid Filename 如何通过Google Cloud Storage对Java应用程序进行身份验证? - How do I authenticate my Java application with Google Cloud Storage? 如何在本地开发环境中从App Engine访问Google云端存储? - How to access Google Cloud Storage from App Engine in local development environment? 如何使用Java从本地开发服务器访问生产数据存储区? - How do I access production Datastore from my local development server using Java? 如何将Firestore数据导出到Google Cloud Storage? - How do I export Firestore data to Google Cloud Storage? 如何获取本地开发服务器中Google云存储上载文件的公共链接(Google App Engine + JAVA) - How to get public link for the uploaded file on google cloud storage in local dev server(Google App engine+JAVA) 如何轻松地将本地文件发送到 Scala 中的 Google Cloud Storage - How to easily send local file to Google Cloud Storage in Scala 如何在App Engine上使用Google Client Library for Java创建Google Cloud Storage可恢复上传网址? - How do I create a Google Cloud Storage resumable upload URL with Google Client Library for Java on App Engine? 使用 Google Cloud Spring 依赖项进行本地开发 - Local development with Google Cloud Spring dependencies 如何从 AWS 云开发工具包中的代码引用我的 lambda? - How do I reference my lambda from code in AWS Cloud Development Kit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM