【问题标题】:Accessing Cloud Storage from Android从 Android 访问云存储
【发布时间】:2017-07-24 22:09:45
【问题描述】:

我一直找不到任何关于如何从 Android 应用程序使用 Cloud Storage 的具体文档。

我确实遇到了来自 Google Cloud SDK 的 this client library,但是遇到了很多很多问题,但尚未使其正常工作。

我按照上面链接中的建议添加了以下代码:

build.gradle

compile group: 'com.google.cloud', name: 'google-cloud-storage', version: '0.9.3-beta'

然后我添加了一些简单的代码,尽管这与这个问题并不真正相关,因为我还没有能够运行我的应用程序并添加了上述依赖项:

在活动中:

Storage storage = StorageOptions.getDefaultInstance().getService();
Page<Bucket> buckets = storage.list();
Iterator<Bucket> bucketIterator = buckets.iterateAll();
while (bucketIterator.hasNext()) {
    Bucket bucket = bucketIterator.next();
    Log.d(TAG, "Bucket name: " + bucket.getName());
}

在解决了无数的依赖问题(与 Joda、Netty、DuplicateFileException's from gradle 的冲突等)之后,我能够构建项目,尽管出现以下错误:

Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for debug as it may be conflicting with the internal version provided by Android. Warning:WARNING: Dependency org.json:json:20151123 is ignored for debug as it may be conflicting with the internal version provided by Android.

然后我可以尝试运行,它会失败并出现数百个错误,其中大部分看起来如下所示:

Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.google.inject.internal.cglib.reflect.$FastClassEmitter$3) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.

经过相当多的这些,具有不同的类名,错误的结尾包含以下内容:

处理“javax/transaction/HeuristicCommitException.class”的问题:

在以下情况下对核心类(java.* 或 javax.*)的不明智或错误使用 不构建核心库。

这通常是由于无意中包含了核心库文件 您的应用程序的项目,当使用 IDE(例如 Eclipse)时。如果 你确定你不是故意定义一个核心类,那么这个 是对正在发生的事情的最可能的解释。

但是,您实际上可能正在尝试在核心中定义一个类 命名空间,您可能已经获取了它的来源,例如,从 非安卓虚拟机项目。这肯定不会 工作。至少,它会危害您的应用程序与 平台的未来版本。它也经常是有问题的 合法性。

如果你真的打算建立一个核心库——这只是 适合作为创建完整虚拟机分发的一部分, 而不是编译应用程序 - 然后使用 “--core-library”选项来禁止显示此错误消息。

如果您继续使用“--core-library”,但实际上是在构建一个 应用程序,然后预先警告您的应用程序仍然会失败 在某个时候构建或运行。请为愤怒的顾客做好准备 例如,他们发现您的应用程序停止运行一次 他们升级他们的操作系统。你将为此负责 问题。

如果您合法地使用某些恰好位于核心中的代码 包装,那么您拥有的最简单安全的替代方法是重新包装 那个代码。也就是说,将有问题的类移动到您自己的包中 命名空间。这意味着它们永远不会与核心冲突 系统类。 JarJar 是一个可以帮助您完成这项工作的工具。 如果你发现你不能做到这一点,那么这表明你 你所走的道路最终会导致痛苦、苦难、悲伤, 和悲叹。

几个问题:

  1. 此客户端库是从我的 Android 应用程序访问我的 Google Cloud Storage 的正确方法吗?
  2. 我是否有理由不尝试从移动应用程序访问云存储?例如,更好的架构是对我的 App Engine 应用程序(使用 Cloud Enpoints)进行 REST API 调用并将媒体对象传递给它,然后让 App Engine 应用程序访问并将媒体存储在 Cloud Storage 中,最后返回结果到移动应用?
  3. 如果我使用上述客户端库正确访问 Cloud Storage,这些错误是什么意思,以及解决方法是什么?

【问题讨论】:

  • 您的用例是什么?正确的访问方法取决于您要执行的操作。
  • 此时我会接受任何类型的与我的云存储的连接。如代码 sn-p 所示,我只是想列出存储桶名称。我的“最终”用例将是上传/下载视频。

标签: java android google-app-engine google-cloud-storage google-cloud-platform


【解决方案1】:

您不应从您的安全令牌无法信任的客户端应用程序直接写入 Cloud Storage。听起来您不打算直接以用户身份进行身份验证,而是通常使用您自己的项目进行身份验证。

如果您不打算执行基于用户的 OAuth2 流程(用户必须已经拥有自己的 Cloud Console 项目并启用了存储),那么您正在寻找通过您自己的后端(例如 App Engine)代理请求。

这种方法可将您的存储凭据安全地存储在服务器端,而不是客户端应用的 APK 中,后者可能会被拆除、提取,然后恶意行为者可以使用这些凭据以他们选择的任何方式写入您的存储桶。哦,存储桶和存储是一种计费资源,因此暴露这些凭据可能会让您付出代价。

这是许多移动云服务的常见模式,您希望通过自己的后端或 API 路由请求。

【讨论】:

  • 所以您是说我需要向 App Engine 应用发出请求,该应用又与 Cloud Storage 通信?无法从移动应用程序直接与 Cloud Storage 通信是没有意义的。例如,Firebase Storage 是 Cloud Storage 的后端,但我可以直接从移动应用程序上传和下载对象。鉴于您的回答,这不是安全问题吗?
  • 是的,如果您要使用 Cloud Storage,这就是我要说的。 Firebase 存储使用 Firebase Authentication for Android 处理安全性,它依赖于我认为位于 GMS 核心库中的 Android 身份验证机制。如果你真的想要云存储,你将不得不推出自己的代理。为什么不使用 Firebase 存储?它仍然是另一项普遍可用的 Google 服务,即使它不是云品牌服务。
  • 我的困惑仍在继续,因为 Firebase Storage 由 Cloud Storage 提供支持。如果我使用 Firebase Storage 存储某些内容,我可以转到 Google Cloud Platform 控制台,导航到 Cloud Storage,然后存储桶就在那里,其中包含使用 Firebase Storage 添加的项目。我不明白为什么直接使用 GCS 会有什么不同。我对使用 Firebase 存储的犹豫是要避免使用尽可能多的不同 3rd 方服务,尤其是当 Firebase 的某些功能可能与 App Engine 发生冲突时(例如 Firebase 侦听器禁用 App Engine 的自动缩放)。
  • 继续我的上一条评论,我希望尽可能只使用 GCP 提供的产品,只是为了让事情变得更容易。我知道 Firebase 确实与 GCP 集成得很好,但它不是 GCP 的一部分,不像 GCP 那样保证,并且添加了另一个似乎不必要的 3rd 方层。
  • Firebase 的 App Engine 自动扩缩问题在于实时数据库和长期连接。对于 Android 的 Firebase Storage,我认为限制不是问题。您当然可以直接使用 GCS 作为我的答案详细信息。最后,Firebase 不是第三方产品。 Google 的 Firebase 旨在帮助移动开发人员使用云服务并隐藏困难的部分。当然,欢迎您以艰难的方式去做。
【解决方案2】:

我在猜测,但我认为 Firebase Storage API 是您应该这样做的方式: https://firebase.google.com/docs/storage/

【讨论】:

  • 我目前没有在我的应用程序的任何地方使用 Firebase。我知道 Firebase Storage 由 Google Cloud Storage 提供支持,但 Firebase 不是 Google Cloud Platform 的产品,我非常怀疑这就是您访问 Cloud Storage 存储桶的方式。不过我很感激这个答案。
【解决方案3】:

这可能是一个迟到的答案。但我认为,它会对某人有用。

首先,您应该通过在 GCP 控制台中注册您的应用程序来获取以下信息。

private final String pkcsFile = "xxx.json";//private key file
private final String bucketName = "your_gcp_bucket_name";
private final String projectId = "your_gcp_project_id";

获得凭据后,您应该将私钥(.p12 或 .json)放入您的资产文件夹中。我正在使用 JSON 格式的私钥文件。此外,您应该更新要上传的图像位置。

@RequiresApi(api = Build.VERSION_CODES.O)
    public void uploadImageFile(String srcFileName, String newName) {
            Storage storage = getStorage();

        File file = new File(srcFileName);//Your image loaction
        byte[] fileContent;
        try {
            fileContent = Files.readAllBytes(file.toPath());
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        if (fileContent == null || fileContent.length == 0)
            return;
        BlobInfo.Builder newBuilder = Blob.newBuilder(BucketInfo.of(bucketName), newName);
        BlobInfo blobInfo = newBuilder.setContentType("image/png").build();
        Blob blob = storage.create(blobInfo, fileContent);
        String bucket = blob.getBucket();
        String contentType = blob.getContentType();
        Log.e("TAG", "Upload File: " + contentType);
        Log.e("File ", srcFileName + " uploaded to bucket " + bucket + " as " + newName);
    }

private Storage getStorage() {
        InputStream credentialsStream;
        Credentials credentials;

        try {
            credentialsStream = mContext.getAssets().open(pkcsFile);
            credentials = GoogleCredentials.fromStream(credentialsStream);

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return StorageOptions.newBuilder()
                .setProjectId(projectId).setCredentials(credentials)
                .build().getService();
    }

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 1970-01-01
    • 2019-08-24
    • 2021-08-21
    • 2023-03-05
    • 1970-01-01
    • 2017-05-13
    • 2019-12-12
    • 2021-04-25
    相关资源
    最近更新 更多