全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

简单上传(Node.js)

最近更新时间:2024-10-24 19:18:17

简单上传文件

client.object.put({
    Bucket: '<bucketName>', // 填入存储桶,非必填
    Key: '<objectKey>',// 填入对象键(例如1.jpg,a/b/test.txt),必填字段
    Body: fs.createReadStream(filePath), // 上传文件对象,必须字段
    StorageClass: 'STANDARD', //存储类型,非必填
    ACL: 'private', //对象的预定义权限,非必填
    headers: {
        'Content-Length':fs.statSync(filePath) //当 Body 为 stream 类型时,ContentLength 必传
    }
}, function(err, data, res, body) {
    if (err) {
        console.log('==========> err:', err)
    } else {
        console.log('==========> data:', JSON.stringify(data))
        console.log('==========> statusCode:', res.statusCode)
    }
})

上传Buffer作为文件内容

client.object.put({
    Bucket: '<bucketName>', // 填入存储桶,非必填
    Key: '<objectKey>',// 填入对象键(例如1.jpg,a/b/test.txt),必填字段
    Body: Buffer.from('this is a buffer content'), // 上传文件对象,必须字段
    ServerSideEncryption: true, //是否使用服务端加密,非必填
}, function(err, data, res, body) {
    if (err) {
        console.log('==========> err:', err)
    } else {
        console.log('==========> data:', JSON.stringify(data))
        console.log('==========> statusCode:', res.statusCode)
    }
})

上传字符串作为文件内容

client.object.put({
    Bucket: '<bucketName>', // 填入存储桶,非必填
    Key: '<objectKey>',// 填入对象键(例如1.jpg,a/b/test.txt),必填字段
    Body: '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><title>测试</title></head><body>testing page for gulp-ks3</body></html>', // 上传文件对象,必须字段
    onProgress: function(progressData) {
        console.log(JSON.stringify(progressData));
    } //获取上传进度
}, function(err, data, res, body) {
    if (err) {
        console.log('==========> err:', err)
    } else {
        console.log('==========> data:', JSON.stringify(data))
        console.log('==========> statusCode:', res.statusCode)
    }
})

创建目录a

client.object.put({
    Bucket: '<bucketName>', // 填入存储桶,非必填
    Key: 'a/'// 填入对象键,必填字段
}, function(err, data, res, body) {
    if (err) {
        console.log('==========> err:', err)
    } else {
        console.log('==========> data:', JSON.stringify(data))
        console.log('==========> statusCode:', res.statusCode)
    }
})

自定义Headers

client.object.put({
    Bucket: '<bucketName>', // 填入存储桶,非必填
    Key: '<objectKey>',// 填入对象键(例如1.jpg,a/b/test.txt),必填字段
    Body: Buffer.from('this is a buffer content') // 上传文件对象,必须字段
    headers: {
      'x-kss-server-side-encryption-customer-algorithm': 'AES256',
      'x-kss-server-side-encryption-customer-key': 'customer-key',
      'x-kss-server-side-encryption-customer-key-MD5': 'customer-key-MD5'
    }// 支持自定义headers 非必须
}, function(err, data, res, body) {
    if (err) {
        console.log('==========> err:', err)
    } else {
        console.log('==========> data:', JSON.stringify(data))
        console.log('==========> statusCode:', res.statusCode)
    }
})

上传文件到指定目录a/b/objectKey

client.object.put({
    Bucket: '<bucketName>', // 填入存储桶,非必填
    Key: 'a/b/objectKey',// 填入对象键,必填字段
    Body: Buffer.from('this is a buffer content') // 上传文件对象,必须字段
}, function(err, data, res, body) {
    if (err) {
        console.log('==========> err:', err)
    } else {
        console.log('==========> data:', JSON.stringify(data))
        console.log('==========> statusCode:', res.statusCode)
    }
})

文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈