Files
mp-10584/src/utils/oss.js
赵忠林 482e2a2718 chore(config): 添加项目配置文件和隐私协议
- 添加 .editorconfig 文件统一代码风格
- 添加 .env.development 和 .env.example 环境配置文件
- 添加 .eslintignore 和 .eslintrc.js 代码检查配置
- 添加 .gitignore 版本控制忽略文件配置
- 添加 .prettierignore 格式化忽略配置
- 添加隐私协议HTML文件
- 添加API密钥管理组件基础结构
2026-01-26 14:05:01 +08:00

44 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import OSS from 'ali-oss';
const baseUrl = 'http://oss-aishangjia.oss-cn-shenzhen.aliyuncs.com';
export const uploadAliOss = async (file, options) => {
// 构建上传文件参数
// 获取上传文件所需要的STS Token
// 直接通过node.js上传
// console.log(token)
const client = new OSS({
region: 'oss-cn-shenzhen',
accessKeyId: 'LTAI4GKGZ9Z2Z8JZ77c3GNZP',
accessKeySecret: 'BiDkpS7UXj72HWwDWaFZxiXjNFBNCM',
bucket: 'oss-aishangjia',
secure: true
});
const headers = {
// 指定该Object被下载时的网页缓存行为。
'Cache-Control': 'no-cache',
// 指定该Object被下载时的名称。
// 指定该Object被下载时的内容编码格式。
'Content-Encoding': 'utf-8',
// 指定过期时间,单位为毫秒。
Expires: '1000',
// 指定Object的存储类型。
'x-oss-storage-class': 'Standard',
// 指定初始化分片上传时是否覆盖同名Object。此处设置为true表示禁止覆盖同名Object。
'x-oss-forbid-overwrite': 'true'
};
const suffix = file.name.substring(file.name.lastIndexOf('.')); // .txt
const objectName = Date.now() + suffix;
// object-name可以自定义为文件名例如file.txt或目录例如abc/test/file.txt的形式实现将文件上传至当前Bucket或Bucket下的指定目录。
const result = await client.multipartUpload(objectName, file, {
...options,
parallel: 4,
partSize: 1024 * 1024 * 5
});
result.url = `${baseUrl}/${result.name}`;
return Promise.resolve(result);
};