3.1 KiB
3.1 KiB
sirv(500 mb storage)
-
https://apidocs.sirv.com nodejs
const http = require("https"); const options = { "method": "POST", "hostname": "api.sirv.com", "port": null, "path": "/v2/token", "headers": { "content-type": "application/json", "authorization": "Bearer BEARER_TOKEN_HERE" } }; const req = http.request(options, function (res) { const chunks = []; res.on("data", function (chunk) { chunks.push(chunk); }); res.on("end", function () { const body = Buffer.concat(chunks); console.log(body.toString()); }); }); req.write("{\"clientId\":\"ZcnZNfzwRhQExoHFoGpxWJ4p2R\",\"clientSecret\":\"TM3d0CfXxusKMpH3x7kHJYD40qJIR3omTIGXP6wPPkXpIUKLEz/dOJ9v6LbXra3y67XsaGK7iQPmnAuD+fzj+Q==\"}"); req.end();Javascript(Vanilla)
const data = "{\"clientId\":\"ZcnZNfzwRhQExoHFoGpxWJ4p2R\",\"clientSecret\":\"TM3d0CfXxusKMpH3x7kHJYD40qJIR3omTIGXP6wPPkXpIUKLEz/dOJ9v6LbXra3y67XsaGK7iQPmnAuD+fzj+Q==\"}"; const xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.addEventListener("readystatechange", function () { if (this.readyState === this.DONE) { console.log(this.responseText); } }); xhr.open("POST", "https://api.sirv.com/v2/token"); xhr.setRequestHeader("content-type", "application/json"); xhr.setRequestHeader("authorization", "Bearer BEARER_TOKEN_HERE"); xhr.send(data);
to upload file with curl
curl --request POST \
--url 'https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt' \
--header 'authorization: Bearer BEARER_TOKEN_HERE' \
--header 'content-type: application/octet-stream' \
--data-binary @path/to/your/local/file.txt
with nodejs
const fetch = require("node-fetch");
const fs = require("fs");
const path = require("path");
const uploadFile = async () => {
try {
// Replace with your own details
const apiURL = "https://api.sirv.com/v2/files/upload?filename=%2FREST%20API%20Examples%2Fuploaded.txt";
const token = "BEARER_TOKEN_HERE";
const filePath = path.join(__dirname, "your-local-file.txt"); // Path to your local file
// Read the file as binary data
const fileStream = fs.createReadStream(filePath);
// Make the POST request with binary data
const response = await fetch(apiURL, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/octet-stream", // Appropriate content-type for binary file uploads
},
body: fileStream, // Use the file stream as the request body
});
if (response.ok) {
const data = await response.json();
console.log("File uploaded successfully:", data);
} else {
const error = await response.text();
console.error("Error uploading file:", error);
}
} catch (error) {
console.error("Error uploading file:", error.message);
}
};
// Start the file upload
uploadFile();
sharable link format https://platletinvt.sirv.com/Images/Apple%20Watch%20white.jpg where this jpg file is in Images folder platletinvt is username Images is folder name rest is filename