-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
64 lines (53 loc) · 1.86 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { Storage } = require("@google-cloud/storage");
const fetch = require("node-fetch");
async function uploadFileFromUrl(url, bucketName, destinationFileName) {
// Create a new instance of the Storage client
const storage = new Storage();
try {
// Get a reference to the bucket
const bucket = storage.bucket(bucketName);
// Get a reference to the file
const file = bucket.file(destinationFileName);
// Check if the file exists and delete it if it does
const [exists] = await file.exists();
if (exists) {
await file.delete();
console.log(`Existing file ${destinationFileName} deleted.`);
}
// Fetch the file content from the URL
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const fileStream = response.body;
// Create a write stream for the destination file
const writeStream = file.createWriteStream({
resumable: false,
gzip: true,
public: true,
});
// Pipe the file content to Google Cloud Storage
fileStream.pipe(writeStream);
return new Promise((resolve, reject) => {
writeStream.on("finish", () => {
console.log(`File ${destinationFileName} uploaded successfully.`);
resolve();
});
writeStream.on("error", (error) => {
console.error("Error uploading file:", error);
reject(error);
});
});
} catch (error) {
console.error("Error:", error);
throw error;
}
}
// Usage example
const fileUrl =
"https://media.kubric.io/api/modemagic/sidecartv2e5d88d8121239d4d6ca8b4f27950784f.js";
const bucketName = "modemagic";
const destinationFileName = "sidecart_test.js";
uploadFileFromUrl(fileUrl, bucketName, destinationFileName)
.then(() => console.log("Upload completed"))
.catch((error) => console.error("Upload failed:", error));