java - 从 Android 访问云存储

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

我一直无法找到有关如何从 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/

相关文章:

java - 外卡子域 apache+tomcat

java - 如果变量为空,则公式失败

android - Android Studio 无法识别 "RobolectricTestRunner.class"

google-app-engine - 升华 : No code completion for App Engine packages

java - Weblogic JPA 2.1 Hibernate 4 不工作 java.lang.NoSuchMethodError

java - maven2 中的替代插件执行?

java - HttpURLConnection 抛出 java.io.IOException : insufficient data written

android - 什么时候在 Flutter 中使用 setState?

ruby-on-rails - 如何使用 Sendgrid API 发送电子邮件以使用 rails 4 发送电子邮件?我似乎无法把所有的部分放在一起

java - IntelliJ 中的 JSTL 在 JSP 中给出错误