上传文件到云存储空间

参数
| 名称 | 类型 | 必须 | 说明 | 
|---|---|---|---|
| option | Object | 是 | |
| option.cloudPath | String | 是 | 云存储文件存储路径 | 
| option.fileContent | Buffer/ReadableStream | 是 | 文件内容 | 
| option.contentType | String | 是 | 文件 Conetent-Type | 
| option.contentLength | String | 是 | 文件:Content-Length | 
返回值 Promise
resolve 结果:
| 名称 | 类型 | 说明 | 
|---|---|---|
| requestID | String | 请求 ID | 
| fileID | String | 文件 ID | 
| statusCode | Number | 云存储服务器返回状态码 | 
reject 结果:
| 名称 | 类型 | 说明 | 
|---|---|---|
| code | String | 错误码 | 
| message | String | 错误信息 | 
代码示例
const cloud = require('swan-server-sdk')
const path = require('path')
const fs = require('fs')
const util = require('util')
exports.main = async (event, context) => {
cloud.init(context)
const filePath = path.join(__dirname, 'demo.jpg');
const fileStream = fs.createReadStream(filePath);
const fileStats = await util.promisify(fs.stat)(filePath)
const res = await cloud.storage().uploadFile({
cloudPath: 'photo/demo.jpg',
fileContent: fileStream,
contentType: 'image/jpeg',
contentLength: fileStats.size
})
return res.fileID
}