Giter VIP home page Giter VIP logo

cos-xml-react-native's Introduction

COS XML React Native SDK

安装 SDK

npm install cos-xml-react-native

如果你使用 yarn

yarn add cos-xml-react-native

如果没有自动执行 pod 安装,请执行:

cd ios
pod install

开始使用

1. 导入 SDK

import CosXmlReactNative from 'cos-xml-react-native';

2. 初始化服务

使用临时密钥初始化

// 使用临时密钥初始化
CosXmlReactNative.initWithSessionCredentialCallback({
  region: region
}, async () => {
  // 请求临时密钥
  const response = await fetch(STS_URL);
  const responseJson = await response.json();
  const credentials = responseJson.credentials;
  const expiredTime = responseJson.expiredTime;
  const sessionCredentials = {
    tmpSecretId: credentials.tmpSecretId,
    tmpSecretKey: credentials.tmpSecretKey,
    expiredTime: expiredTime,
    sessionToken: credentials.sessionToken
  };
  console.log(sessionCredentials);
  return sessionCredentials;
})

使用永久密钥进行本地调试

const SECRET_ID: string = 'Your Secret ID'  // 腾讯云永久密钥 SecretID
const SECRET_KEY: string = 'Your Secret Key'  // 腾讯云永久密钥 SecretKey
// 使用永久密钥进行本地调试
CosXmlReactNative.initWithPlainSecret({
  region: region
}, {
  secretId: SECRET_ID,
  secretKey: SECRET_KEY
})

访问 COS 服务

上传文件

const uploadRequest = {
  bucket: bucket,
  object: objectKey,
  // 文件本地 Uri 或者 路径
  fileUri: 'file://xxx'
}

// 上传 与 暂停后续传对象
CosXmlReactNative.upload(uploadRequest, 
  (processedBytes: number, targetBytes: number) => {
    // 回调进度
    console.log('put Progress callback : ', processedBytes, targetBytes)
    setProgress(processedBytes / targetBytes)
}).then((info) => {
  // info 包含上传结果
  console.log(info)
}).catch((e) => {
  console.log(e)
})

暂停上传请调用:

CosXmlReactNative.pause(uploadRequest)

下载文件

// 下载对象
CosXmlReactNative.download({
  bucket: bucket,
  object: objectKey
}, (processedBytes: number, targetBytes: number) => {
  // 回调进度
  console.log('get Progress callback : ', processedBytes, targetBytes)
  setProgress(processedBytes / targetBytes)
}).then((filePath) => {
  // filePath 是保存到本地的路径
  setSource({uri: "file://" + filePath})
}).catch((e) => {
  console.log(e)
})

示例

完整的例子请参考 example 示例工程。

cos-xml-react-native's People

Contributors

konakonall avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.