草庐IT

在快应用中集成华为AGC云存储服务

华为开发者论坛 2023-03-28 原文

目前华为AGC云存储服务已经支持在快应用中集成了,你可以使用云存储服务存储图片、视频、音频等,集成的Demo可以参考Github

1、安装Node.js环境:

1、下载Node.js安装包:https://nodejs.org/en/download/

2、Window环境安装Node.js

3、安装好以后,系统会弹出一个cmd框提示你自动安装所需的插件

 

2、检查PATH环境变量内是否配置了NodeJS:

1、我的电脑 – 右键选择属性 – 选择高级系统设置 – 选择环境变量 – 查看系统变量

      在系统变量界面选择Path:查看是否有安装的NodeJS路径:

        

2、查看NodeJS版本; 查看npm版本

       

3、安装快应用IDE并且配置环境

1、下载并且安装快应用IDE,与快应用加载器

      https://developer.huawei.com/consumer/cn/doc/development/quickApp-Guides/quickapp-installtool

2、IDE操作: 在IDE创建一个快应用项目:

3、点击 Npm > 启动第三方NPM库,此时IDE会自动向项目中添加fa-toolkit以及package.json。

 

 

4、SDK集成

1、下载agconnect-services.json文件,并且放到src目录下

 

2、执行npm命令,添加云存储依赖项:npm install --save @agconnect/cloudstorage

               

3、安装依赖,点击IDE菜单“Npm > 安装依赖”

 

5、功能开发

a) 界面设计与前置步骤

1、在src/manifest.json文件中,features下添加system.media依赖,用于获取本地文件

1
2
3
{
  "name": "system.media"
}
 

2、在src/Hello/hello.ux文件中,添加登录按钮,并且添加匿名登录的相关代码:

3、点击IDE菜单“文件 > 新建页面”,页面路径为“Main”,页面文件名为“index”,添加界面布局。

可以按照下图进行UI设计。

 

b) 创建引用

1、src/app.ux文件中,添加编译依赖配置和onCreate函数下初始化agc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script>
import agconnect from "@agconnect/api";
  import "@agconnect/cloudstorage";
  
  module.exports = {
    onCreate() {
      console.info('Application onCreate');
      let agConnectConfig = require('./agconnect-services.json');
      agconnect.instance().configInstance(agConnectConfig);
    },
    onDestroy() {
      console.info('Application onDestroy');
    },
    dataApp: {
      localeData: {}
    },
    agc: agconnect
  }
</script>
 

c) 上传文件

putFile为上述UI中putFile按钮绑定函数,可根据自身设计代码调整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
putFile() {
  let that = this;
  media.pickFile({
    success: function (data) {
      console.log("handling success: " + data.uri);
      let agconnect = that.$app.$def.agc;
      let ref = agconnect.cloudStorage().storageReference();
      let path = that.currentPath + that.getName(data.uri);
      const child = ref.child(path);
      child.put4QuickApp(data.uri, {
        cacheControl: 'helloworld',
        contentDisposition: 'helloworld',
        contentEncoding: 'helloworld',
        contentLanguage: 'helloworld',
        contentType: 'helloworld',
        customMetadata: {
          hello: 'kitty'
        }
      }).then((res) => {
        that.result = JSON.stringify(res, null"\t");
        prompt.showToast({
          message: `putFile success`,
          duration: 3500,
          gravity: 'center'
        });
      })
    },
 

d) 获取文件列表

getList、getListAll为上述UI中对应按钮的绑定函数,可根据自身设计代码调整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
getList() {
  let agconnect = this.$app.$def.agc;
  let ref = agconnect.cloudStorage().storageReference();
  let path = this.selected === '' this.currentPath : this.selected;
  const child = ref.child(path);
  child.list({ maxResults: 10 }).then((res) => {
    this.currentPath = path;
    this.selected = '';
    this.setList(res);
  })
},
getListAll() {
  let agconnect = this.$app.$def.agc;
  let ref = agconnect.cloudStorage().storageReference();
  let path = this.selected === '' this.currentPath : this.selected;
  const child = ref.child(path);
  child.listAll().then((res) => {
    this.currentPath = path;
    this.selected = '';
    this.setList(res);
  })
},
 

e) 获取文件下载地址

getDownloadURL为上述UI中对应按钮的绑定函数,可根据自身设计代码调整。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
getDownloadURL() {
  if (this.selected === '' || this.selected.endsWith('/')) {
    prompt.showToast({
      message: `only file can getDownloadURL`,
      duration: 3500,
      gravity: 'center'
    });
    return;
  }
  let agconnect = this.$app.$def.agc;
  let ref = agconnect.cloudStorage().storageReference();
  const child = ref.child(this.selected);
  child.getDownloadURL().then((res) => {
    this.result = res;
    prompt.showToast({
      message: `getDownloadURL success`,
      duration: 3500,
      gravity: 'center'
    });
  })
}
 

5、现象与验证

在快应用IDE中间点击Run,运行该快应用,可以在右侧查看相应的效果

  

 

6、总结

CloudStorage之前的JS SDK,已经无缝支持华为快应用,多场景的覆盖更加全面。另外在快应用的使用上方便快捷,API接口齐全满足各种使用情况,

 

详细开发指南:https://developer.huawei.com/consumer/cn/doc/development/AppGallery-connect-Guides/agc-cloudstorage-getstarted-quickapp

云存储快应用Demo:

https://github.com/AppGalleryConnect/agc-demos/tree/main/Quickapp/CloudStorage

 

原文链接:https://developer.huawei.com/...
原作者:Mayism

有关在快应用中集成华为AGC云存储服务的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  6. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  7. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

  8. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  9. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  10. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

随机推荐