Copy images from one folder to another in digitalocean spaces

Viewed 405

I am working with digital ocean spaces and I have uploaded the images on it in a temporary folder. Now I want to move that image from temporary folder to permanent folder. I have searched the things from all over but got nothing much satisfying. Is it possible to do so ? and if yes please help me with the javascript code.

First I generated the signed url and with the help of that signed url I uploaded the image on digitalocean spaces. Following is the code for generating signed url and uploading images.

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
}

please help me how can I move my image from temporary folder to permanent folder.

1 Answers

So finally after searching I got the answer, From the front end side I call an API to copy my image

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);
    }
}

And in the backend side the API I made is

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 })
        }
    });
});

This will copy you image in original folder

Related