Ali OSS does not support folder uploads by default, only single file uploads are allowed. If you need to upload folders, you can use either of the following methods: 1. You can upload files one by one through the folder. 2. Compress the file and upload it.
NPM package to use
ali-oss
compressing
Start by creating a new uploados.js file with the following code.
Upload method provided by OSS
const packageJSON = JSON.parse(fs.readFileSync("package.json"."utf8"));
const remotePath = `/ossDir/${packageJSON.version}/ `;
const localPath = "./localhostDir/";
const client = new OSS({
endpoint: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX".accessKeyId: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX".accessKeySecret: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX".bucket: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"});async function uploadDir(file) {
try {
// Enter the full path of Object and the full path of local files. Object the full path cannot contain a Bucket name.
// If the local path is not specified, the file is uploaded from the local path of the sample program project by default.
// The path of file is backslash by default and needs to be changed to forward slash.
const formatFile = path.normalize(file).replace(/\\/g."/");
const remotePaths = remotePath + formatFile;
const result = await client.put(remotePaths, formatFile);
if (result.res.status === 200) {
console.log("UPLOAD SUCCESS:"+ formatFile); }}catch (e) {
console.log(e); }}async function upload(file, uploadedName) {
try {
// Enter the full path of Object and the full path of local files. Object the full path cannot contain a Bucket name.
// If the local path is not specified, the file is uploaded from the local path of the sample program project by default.
// The path of file is backslash by default and needs to be changed to forward slash.
const formatFile = path.normalize(file).replace(/\\/g."/");
const remotePaths = remotePath + uploadedName;
const result = await client.put(remotePaths, formatFile);
if (result.res.status === 200) {
console.log("UPLOAD SUCCESS:"+ formatFile); }}catch (e) {
console.log(e); }}Copy the code
Clear folders before uploading
async function deleteAll () {
const result = await client.list({
prefix: remotePath
});
result.objects.forEach(item= > {
console.log("DELETE SUCCESS:" + item.name);
client.delete(item.name);
});
}
Copy the code
Upload folders
function readDir(currentDirPath, callback) {
fs.readdir(currentDirPath, function (err, files) {
if (err) {
throw new Error(err);
}
files.forEach(function (name) {
var filePath = path.join(currentDirPath, name);
var stat = fs.statSync(filePath);
if (stat.isFile()) {
callback(filePath, stat);
} else if(stat.isDirectory()) { readDir(filePath, callback); }}); }); } readDir(localPath,function (filePath, stat) {
uploadDir(filePath);
});
Copy the code
Compress the file before uploading it
async function compressFolderAndUpload(path) {
const fileName = `v${packageJSON.version}.tar`;
const toPath = `./compress/${fileName}`;
await compressing.tar.compressDir(path, toPath);
upload(toPath, fileName);
}
compressFolderAndUpload(localPath);
Copy the code