cloud.uploadFile

上传文件到云存储空间

参数

名称类型必须说明
optionObject
option.cloudPathString云存储文件存储路径
option.fileContentBuffer/ReadableStream文件内容
option.contentTypeString文件 Conetent-Type
option.contentLengthString文件:Content-Length

返回值 Promise

resolve 结果:

名称类型说明
requestIDString请求 ID
fileIDString文件 ID
statusCodeNumber云存储服务器返回状态码

reject 结果:

名称类型说明
codeString错误码
messageString错误信息

代码示例

  1. const cloud = require('swan-server-sdk')
  2. const path = require('path')
  3. const fs = require('fs')
  4. const util = require('util')
  5. exports.main = async (event, context) => {
  6. cloud.init(context)
  7. const filePath = path.join(__dirname, 'demo.jpg');
  8. const fileStream = fs.createReadStream(filePath);
  9. const fileStats = await util.promisify(fs.stat)(filePath)
  10. const res = await cloud.storage().uploadFile({
  11. cloudPath: 'photo/demo.jpg',
  12. fileContent: fileStream,
  13. contentType: 'image/jpeg',
  14. contentLength: fileStats.size
  15. })
  16. return res.fileID
  17. }