javascript - 在数字海洋空间中将图像从一个文件夹复制到另一个文件夹
问题描述
我正在使用数字海洋空间,并且已将其上的图像上传到一个临时文件夹中。现在我想将该图像从临时文件夹移动到永久文件夹。我从四面八方找遍了东西,但没有什么令人满意的。有可能这样做吗?如果是,请帮助我处理 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
}
请帮助我如何将我的图像从临时文件夹移动到永久文件夹。
解决方案
所以最后在搜索后我得到了答案,从前端我调用一个 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 })
}
});
});
这会将您的图像复制到原始文件夹中
推荐阅读
- java - 模拟对不同方法的一系列调用
- amazon-web-services - 是否可以使用 BlockJUnit4ClassRunner 将单元测试迁移到 Junit 5?
- javascript - 将视频播放时间保存到数据库
- json - jq获取父子键对孙值进行计算
- java - 如何创建不唯一的主关联键
- attributes - Modelica:从方程中获取标称值
- shiny - 仅在 prod 中使用 shinyproxy 的 downloadhandler 错误
- c# - 为什么输出是 6 而不是其他数字?
- java - 如何解析mif文件?
- javascript - 动态表单提交重新加载页面而不是调用 ngSubmit 函数