Skip to content

Commit db1d8b3

Browse files
authored
Merge pull request supabase#5142 from supabase/docs/migrate-storage-objects
Docs: migrate storage objects
2 parents 6da53a7 + 5e1992e commit db1d8b3

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

web/docs/guides/database.mdx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,73 @@ Migrating projects can be achieved using standard PostgreSQL tooling. This is pa
138138
6. Run `TRUNCATE storage.objects` in the *new* project's SQL editor
139139
7. Run `ALTER ROLE postgres NOSUPERUSER` in the *new* project's SQL editor
140140

141+
#### Migrate storage objects
142+
143+
This script moves storage objects from one project to another. If you have more than 10k objects, we can move the objects for you. Just contact us at [[email protected]](mailto:[email protected]).
144+
145+
```js
146+
const { createClient } = require("@supabase/supabase-js");
147+
148+
const OLD_PROJECT_URL = "https://xxx.supabase.co";
149+
const OLD_PROJECT_SERVICE_KEY = "old-project-service-key-xxx";
150+
151+
const NEW_PROJECT_URL = "https://yyy.supabase.co";
152+
const NEW_PROJECT_SERVICE_KEY = "new-project-service-key-yyy";
153+
154+
(async () => {
155+
const oldSupabaseRestClient = createClient(
156+
OLD_PROJECT_URL,
157+
OLD_PROJECT_SERVICE_KEY,
158+
{ schema: "storage" }
159+
);
160+
const oldSupabaseClient = createClient(
161+
OLD_PROJECT_URL,
162+
OLD_PROJECT_SERVICE_KEY
163+
);
164+
const newSupabaseClient = createClient(
165+
NEW_PROJECT_URL,
166+
NEW_PROJECT_SERVICE_KEY
167+
);
168+
169+
// make sure you update max_rows in postgrest settings if you have a lot of objects
170+
// or paginate here
171+
const { data: oldObjects, error } = await oldSupabaseRestClient
172+
.from("objects")
173+
.select();
174+
if (error) {
175+
console.log("error getting objects from old bucket");
176+
throw error;
177+
}
178+
179+
for (const objectData of oldObjects) {
180+
console.log(`moving ${objectData.id}`);
181+
try {
182+
const { data, error: downloadObjectError } =
183+
await oldSupabaseClient.storage
184+
.from(objectData.bucket_id)
185+
.download(objectData.name);
186+
if (downloadObjectError) {
187+
throw downloadObjectError;
188+
}
189+
190+
const { _, error: uploadObjectError } = await newSupabaseClient.storage
191+
.from(objectData.bucket_id)
192+
.upload(objectData.name, data, {
193+
upsert: true,
194+
contentType: objectData.metadata.mimetype,
195+
cacheControl: objectData.metadata.cacheControl,
196+
});
197+
if (uploadObjectError) {
198+
throw uploadObjectError;
199+
}
200+
} catch (err) {
201+
console.log("error moving ", objectData);
202+
console.log(err);
203+
}
204+
}
205+
})();
206+
```
207+
141208
#### Caveats
142209

143210
- The new project will have the old project's Storage buckets, but not the objects. You will need to migrate Storage objects manually.

0 commit comments

Comments
 (0)