首页 > 解决方案 > 在数字海洋空间中将图像从一个文件夹复制到另一个文件夹

问题描述

我正在使用数字海洋空间,并且已将其上的图像上传到一个临时文件夹中。现在我想将该图像从临时文件夹移动到永久文件夹。我从四面八方找遍了东西,但没有什么令人满意的。有可能这样做吗?如果是,请帮助我处理 javascript 代码。

首先,我生成了签名的 url,并在该签名的 url 的帮助下将图像上传到了 digitalocean 空间。以下是生成签名网址和上传图片的代码。

const getSignedUrl = async () => {
    const body = {
        fileName: 'temp/' + file.name,
        fileType: file.type,
    }

    const response = await fetch(`${API_URL}/presigned_url`, {
        method: 'POST',
        body: JSON.stringify(body),
        headers: { 'Content-Type': 'application/json' }
    })
    const { signedUrl } = await response.json()

    return signedUrl
}

const uploadFile = async signedUrl => {
    const res = await fetch(signedUrl, {
        method: 'PUT',
        body: file,
        headers: {
            'Content-Type': file.type,
            'x-amz-acl': 'public-read',
        }
    })
    return res
}

请帮助我如何将我的图像从临时文件夹移动到永久文件夹。

标签: javascriptnode.jsdigital-ocean

解决方案


所以最后在搜索后我得到了答案,从前端我调用一个 API 来复制我的图像

const copyFile = async (file) => {
    try {
        const body = {
            fileName: file.name
        }
        const res = await fetch(`${API_URL}/copy_file`, {
            method: 'PUT',
            body: JSON.stringify(body),
            headers: { 'Content-Type': 'application/json' }
        })
        return res
    } catch (error) {
        console.log(error);
    }
}

在后端,我制作的 API 是

app.put('/copy_file', (req, res) => {
    const fileName = req.body.fileName
    console.log("body", fileName);
    const params = {
        Bucket: config.spaces.spaceName,
        CopySource: `/bucketName/temp/${fileName}`,
        Key: `original/${fileName}`,
    }
    spaces.copyObject(params, function (err, data) {
        if (err) {
            console.log("Error", err)
            // console.log(err, err.stack); // an error occurred
        } else {
            res.json({ data })
        }
    });
});

这会将您的图像复制到原始文件夹中


推荐阅读