Storage
Fayllar saqlashni boshqarish — bucketlar, yuklash, yuklab olish, o'chirish, signed URL bilan vaqtinchalik kirish. Public va private bucketlar qo'llab-quvvatlanadi. REST API va SDK orqali.
Bucketlar yaratish
Loyiha uchun fayl saqlash depolorini (bucket) yaratish service_key bilan amalga oshiriladi. Bucket public:true bo'lsa, autentifikatsiyasiz fayllarni yuklab olish mumkin.
Bucket identifikatori alfanumerik, dash va nuqta bilan boshlash kerak. Misol: rasmlar, media-2024, data_v1.0.
curl -X POST https://storage.identify.uz/v1/<REF>/storage/v1/bucket \
-H 'Content-Type: application/json' \
-H 'apikey: <SERVICE_KEY>' \
-d '{
"id": "rasmlar",
"public": true
}'import { createClient } from "@storagedb/client";
const db = createClient(
"https://storage.identify.uz/v1/<REF>",
"<SERVICE_KEY>"
);
const bucket = await db.storage.createBucket("rasmlar", {
public: true
});
console.log(bucket); // { id: "rasmlar", public: true }Bucketlar ro'yxati
Loyihaning barcha bucketlarini ro'yxatini olish uchun GET so'rovi yuboriladi. Barcha rol (anon, authenticated, service_role) bu so'rovni yuborishga ruxsat.
Qavs orasida har bir bucket id va public xususiyatlari qaytadi.
curl https://storage.identify.uz/v1/<REF>/storage/v1/bucket \ -H 'apikey: <ANON_KEY>'
import { createClient } from "@storagedb/client";
const db = createClient(
"https://storage.identify.uz/v1/<REF>",
"<ANON_KEY>"
);
const buckets = await db.storage.listBuckets();
console.log(buckets);
// [
// { id: "rasmlar", public: true },
// { id: "hujjatlar", public: false }
// ]Fayllarni yuklash
Faylni bucketga yuklash uchun POST so'rovi apikey headerida anon yoki service_role bilan yuboriladi. Yo'l (path) uchinchi qismda belgilanadi: /storage/v1/object/<bucket>/<path>.
Fayl natijalari oldingi bo'lsa yangilanadi (update), yo'q bo'lsa yangi qo'shiladi. Content-Type avtomatik saqlanadi. Fayl xom (binary) tana sifatida yuboriladi — JSON emas.
curl -X POST https://storage.identify.uz/v1/<REF>/storage/v1/object/rasmlar/avatar.png \ -H 'apikey: <ANON_KEY>' \ -H 'Content-Type: image/png' \ --data-binary @avatar.png
import { createClient } from "@storagedb/client";
const db = createClient(
"https://storage.identify.uz/v1/<REF>",
"<ANON_KEY>"
);
// Browser'da File orqali
const file = new File([...], "avatar.png", { type: "image/png" });
const result = await db.storage
.from("rasmlar")
.upload("avatar.png", file, { contentType: "image/png" });
console.log(result);
// { Key: "rasmlar/avatar.png", bucket_id: "rasmlar", name: "avatar.png", ... }Fayllarni yuklab olish
Private (maxfiy) bucketdagi fayllarni yuklab olish uchun autentifikatsiya kerak — apikey headerida anon_key yoki service_key. Public bucketdagi fayllarni hech kim autentifikatsiyasiz yuklab olishi mumkin.
Maxfiy fayl: /storage/v1/object/<bucket>/<path> (JWT bilan). Public fayl: /storage/v1/public/<bucket>/<path> (JWT siz). Response Headers'da Content-Type qaytadi.
curl https://storage.identify.uz/v1/<REF>/storage/v1/object/rasmlar/avatar.png \ -H 'apikey: <ANON_KEY>' \ -o avatar.png
curl https://storage.identify.uz/v1/<REF>/storage/v1/public/rasmlar/rasm.jpg \ -o rasm.jpg
import { createClient } from "@storagedb/client";
const db = createClient(
"https://storage.identify.uz/v1/<REF>",
"<ANON_KEY>"
);
// Private fayl yuklab olish (autentifikatsiya bilan)
const { data, error } = await db.storage
.from("rasmlar")
.download("avatar.png");
if (data) {
const url = URL.createObjectURL(data);
console.log("Fayl URL:", url);
}Fayllarni o'chirish
Faylni bucketdan o'chirish DELETE so'rovi orqali amalga oshiriladi. O'chirish uchun faylni saqlagan foydalanuvchi yoki service_role ruxsati kerak.
Yo'l belgilanadi: /storage/v1/object/<bucket>/<path>. Muvaqqiyatli o'chirish holida HTTP 204 (No Content) qaytadi.
curl -X DELETE https://storage.identify.uz/v1/<REF>/storage/v1/object/rasmlar/avatar.png \ -H 'apikey: <ANON_KEY>'
import { createClient } from "@storagedb/client";
const db = createClient(
"https://storage.identify.uz/v1/<REF>",
"<ANON_KEY>"
);
const { error } = await db.storage
.from("rasmlar")
.remove(["avatar.png"]);
if (error) {
console.error("O'chirish xatosi:", error.message);
} else {
console.log("Fayl o'chirildi");
}Signed URL — vaqtinchalik kirish
Private faylga vaqtinchalik link yaratish uchun signed URL ishlatiladi. Havolaning muddati belgilansa (masalan, 1 soat), o'sha vaqtdan so'ng token ishlamaydi.
Signed URL yaratish /storage/v1/object/sign/<bucket>/<path> POST so'rovi bilan bo'ladi, expiresIn (soniyalarda) ko'rsatiladi. Natijada signedUrl (to'liq URL + token) qaytadi. Qabul qiluvchi autentifikatsiyasiz shu URL orqali faylni yuklab olishi mumkin.
curl -X POST https://storage.identify.uz/v1/<REF>/storage/v1/object/sign/rasmlar/avatar.png \
-H 'apikey: <ANON_KEY>' \
-H 'Content-Type: application/json' \
-d '{
"expiresIn": 3600
}'
# Natija:
# {
# "signedUrl": "https://storage.identify.uz/v1/<REF>/storage/v1/signed/rasmlar/avatar.png?token=1698765432.a1b2c3d4e5f6g7h8...",
# "token": "1698765432.a1b2c3d4e5f6g7h8...",
# "expiresAt": 1698765432
# }# Signed URL avtomatik token bilan, autentifikatsiyasiz ishlatiladi curl 'https://storage.identify.uz/v1/<REF>/storage/v1/signed/rasmlar/avatar.png?token=1698765432.a1b2c3d4e5f6g7h8...' \ -o avatar.png
import { createClient } from "@storagedb/client";
const db = createClient(
"https://storage.identify.uz/v1/<REF>",
"<ANON_KEY>"
);
// 1 soat uchun signed URL yaratish
const { data, error } = await db.storage
.from("rasmlar")
.createSignedUrl("avatar.png", 3600);
if (data) {
console.log("Signed URL:", data.signedUrl);
// Boshqa odam shu URL orqali fayl yuklab olishi mumkin (1 soat ichida)
} else if (error) {
console.error("Xato:", error.message);
}Public URL orqali birlamchi kirish
Agar bucket public: true bo'lsa, fayl to'g'ridan-to'g'ri /storage/v1/public/<bucket>/<path> yo'li orqali autentifikatsiyasiz yuklab olinishi mumkin.
Bu usuli sharh (permanent) havolalar, o'quvchi (read-only) kontenti (suratlar, PDF va h.k.) uchun qulay. Private bucketdagi fayllarni public URL orqali yuklab olib bo'lmaydi — signed URL yoki autentifikatsiyalashgan download kerak.
# Public bucket yaratish
curl -X POST https://storage.identify.uz/v1/<REF>/storage/v1/bucket \
-H 'apikey: <SERVICE_KEY>' \
-H 'Content-Type: application/json' \
-d '{"id": "media", "public": true}'
# Fayl yuklash
curl -X POST https://storage.identify.uz/v1/<REF>/storage/v1/object/media/banner.jpg \
-H 'apikey: <SERVICE_KEY>' \
--data-binary @banner.jpg
# Public URL (autentifikatsiyasiz ham ishlatilishi mumkin)
https://storage.identify.uz/v1/<REF>/storage/v1/public/media/banner.jpgimport { createClient } from "@storagedb/client";
const db = createClient(
"https://storage.identify.uz/v1/<REF>",
"<SERVICE_KEY>"
);
// Public bucket yaratish
await db.storage.createBucket("media", { public: true });
// Fayl yuklash
await db.storage.from("media").upload("banner.jpg", file);
// Public URL olish (o'ziga xos funksiya)
const publicUrl = db.storage
.from("media")
.getPublicUrl("banner.jpg");
console.log(publicUrl.data.publicUrl);
// https://storage.identify.uz/v1/<REF>/storage/v1/public/media/banner.jpg