forked from raunak234362/Ummid-Kiran
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagesToAppwrite.js
More file actions
197 lines (161 loc) · 5.57 KB
/
imagesToAppwrite.js
File metadata and controls
197 lines (161 loc) · 5.57 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// import { Client, Databases, Storage } from "appwrite";
// import fs from "fs";
// import path from "path";
// const client = new Client()
// .setEndpoint("https://cloud.appwrite.io/v1")
// .setProject("67b32231001f01be8e49");
// const databases = new Databases(client);
// const storage = new Storage(client);
// // Function to upload files from a folder
// async function uploadImagesFromFolder(folderPath, bucketId) {
// try {
// // Get all files in the folder
// const files = fs.readdirSync(folderPath);
// for (const fileName of files) {
// // Check if the file is an image (basic check)
// if (/\.(jpg|jpeg|png|gif|webp)$/i.test(fileName)) {
// const filePath = path.join(folderPath, fileName);
// // const fileBuffer = fs.readFileSync(filePath);
// console.log(filePath);
// const fileStream = fs.createReadStream(filePath);
// c
// // Upload to Appwrite
// const file = await storage.createFile(
// bucketId,
// "unique()", // Generate unique ID
// fileStream
// );
// // const file = await storage.createFile(
// // bucketId,
// // "unique()", // Generate unique ID
// // fileBuffer
// // );
// // Get file URL
// const fileUrl = storage.getFileView(bucketId, file.$id);
// // Store in database
// await databases.createDocument(
// "IMAGE_DATABASE_ID",
// "IMAGE_LINKS_COLLECTION_ID",
// "unique()",
// {
// imageUrl: fileUrl,
// bucketId: bucketId,
// fileId: file.$id,
// fileName: fileName,
// }
// );
// console.log(`Uploaded: ${fileName}`);
// }
// }
// console.log("All images uploaded successfully!");
// } catch (error) {
// console.error("Error uploading images:", error);
// }
// }
// // Usage
// uploadImagesFromFolder("./public/gallery", "67c06d8100026343b1ba");
// import { Client, Databases, Storage } from "appwrite";
// import fs from "fs";
// import path from "path";
// const client = new Client()
// .setEndpoint("https://cloud.appwrite.io/v1")
// .setProject("67b32231001f01be8e49");
// const databases = new Databases(client);
// const storage = new Storage(client);
// // Function to upload files from a folder
// async function uploadImagesFromFolder(folderPath, bucketId) {
// try {
// // Get all files in the folder
// const files = fs.readdirSync(folderPath);
// for (const fileName of files) {
// // Check if the file is an image (basic check)
// if (/\.(jpg|jpeg|png|gif|webp)$/i.test(fileName)) {
// const filePath = path.join(folderPath, fileName);
// // Create a read stream (IMPORTANT: Fixes the error)
// const fileStream = fs.createReadStream(filePath);
// // Upload to Appwrite
// const file = await storage.createFile(
// bucketId,
// "unique()", // Generate unique ID
// fileStream
// );
// // Get file URL
// const fileUrl = storage.getFileView(bucketId, file.$id);
// // Store in database
// await databases.createDocument(
// "IMAGE_DATABASE_ID",
// "IMAGE_LINKS_COLLECTION_ID",
// "unique()",
// {
// imageUrl: fileUrl,
// bucketId: bucketId,
// fileId: file.$id,
// fileName: fileName,
// }
// );
// console.log(`Uploaded: ${fileName}`);
// }
// }
// console.log("All images uploaded successfully!");
// } catch (error) {
// console.error("Error uploading images:", error);
// }
// }
// // Usage
// uploadImagesFromFolder("./public/gallery", "67c06d8100026343b1ba");
import { Client, Storage, Databases, ID } from "appwrite";
console.log("appwrite");
import fs from "fs";
import path from "path";
// const fs = require("fs");
// const path = require("path");
const APPWRITE_ENDPOINT = "https://cloud.appwrite.io/v1";
const APPWRITE_PROJECT_ID = "67b32231001f01be8e49";
const BUCKET_ID = "67c06d8100026343b1ba";
const COLLECTION_ID = "67c0856b001a4bfa84af";
const client = new Client()
.setEndpoint(APPWRITE_ENDPOINT)
.setProject(APPWRITE_PROJECT_ID);
// .setKey(APPWRITE_API_KEY);
const storage = new Storage(client);
const database = new Databases(client);
const folderPath = "./public/gallery";
async function uploadImages() {
try {
const files = fs.readdirSync(folderPath);
console.log(files);
for (const file of files) {
const filePath = path.join(folderPath, file);
console.log(filePath);
try {
// Create a read stream for the file
const fileStream = fs.createReadStream(filePath);
const response = await storage.createFile(
BUCKET_ID,
ID.unique(), // Generate a unique file ID
fileStream, // Pass the file stream instead of the file path
["role:all"]
);
if (response) {
const metadata = {
fileName: file,
file: response.$id,
};
const document = await database.createDocument(
COLLECTION_ID,
ID.unique(),
metadata
);
console.log(`File ${file} uploaded and metadata stored:`, document);
} else {
console.error(`Failed to upload file ${file}`);
}
} catch (error) {
console.error(`Error uploading file ${file}:`, error);
}
}
} catch (error) {
console.error("Error reading folder:", error);
}
}
uploadImages();