gpt4 book ai didi

java - 从 Android 访问云存储

转载 作者:行者123 更新时间:2023-11-29 04:33:55 26 4
gpt4 key购买 nike

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

我确实遇到了 this client library来自谷歌云 SDK,但是遇到了很多很多问题,还没有让它工作。

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

build.gradle:

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

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

在 Activity 中:

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 的冲突、来自 gradle 的 DuplicateFileException 等)之后,我能够构建项目,尽管存在以下错误:

警告:警告:依赖项 org.apache.httpcomponents:httpclient:4.0.1 在调试时被忽略,因为它可能与 Android 提供的内部版本冲突。Warning:WARNING: Dependency org.json:json:20151123 is ignored for debug 因为它可能与 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.

在其中一些具有不同类名的错误之后,错误的结尾包含以下内容:

trouble processing "javax/transaction/HeuristicCommitException.class":

Ill-advised or mistaken usage of a core class (java.* or javax.*) when not building a core library.

This is often due to inadvertently including a core library file in your application's project, when using an IDE (such as Eclipse). If you are sure you're not intentionally defining a core class, then this is the most likely explanation of what's going on.

However, you might actually be trying to define a class in a core namespace, the source of which you may have taken, for example, from a non-Android virtual machine project. This will most assuredly not work. At a minimum, it jeopardizes the compatibility of your app with future versions of the platform. It is also often of questionable legality.

If you really intend to build a core library -- which is only appropriate as part of creating a full virtual machine distribution, as opposed to compiling an application -- then use the "--core-library" option to suppress this error message.

If you go ahead and use "--core-library" but are in fact building an application, then be forewarned that your application will still fail to build or run, at some point. Please be prepared for angry customers who find, for example, that your application ceases to function once they upgrade their operating system. You will be to blame for this problem.

If you are legitimately using some code that happens to be in a core package, then the easiest safe alternative you have is to repackage that code. That is, move the classes in question into your own package namespace. This means that they will never be in conflict with core system classes. JarJar is a tool that may help you in this endeavor. If you find that you cannot do this, then that is an indication that the path you are on will ultimately lead to pain, suffering, grief, and lamentation.

几个问题:

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

最佳答案

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

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

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

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

关于java - 从 Android 访问云存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42601917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com