Skip to content

Instantly share code, notes, and snippets.

@inian
Created January 19, 2022 06:18
Show Gist options
  • Save inian/78d2263f40abec6fae9b49ba58ea57f9 to your computer and use it in GitHub Desktop.
Save inian/78d2263f40abec6fae9b49ba58ea57f9 to your computer and use it in GitHub Desktop.

Revisions

  1. inian created this gist Jan 19, 2022.
    60 changes: 60 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    const { createClient } = require("@supabase/supabase-js");

    const OLD_PROJECT_URL = "https://xxx.supabase.co";
    const OLD_PROJECT_SERVICE_KEY = "old-project-service-key-xxx";

    const NEW_PROJECT_URL = "https://yyy.supabase.co";
    const NEW_PROJECT_SERVICE_KEY = "new-project-service-key-yyy";

    (async () => {
    const oldSupabaseRestClient = createClient(
    OLD_PROJECT_URL,
    OLD_PROJECT_SERVICE_KEY,
    { schema: "storage" }
    );
    const oldSupabaseClient = createClient(
    OLD_PROJECT_URL,
    OLD_PROJECT_SERVICE_KEY
    );
    const newSupabaseClient = createClient(
    NEW_PROJECT_URL,
    NEW_PROJECT_SERVICE_KEY
    );

    // make sure you update max_rows in postgrest settings if you have a lot of objects
    // or paginate here
    const { data: oldObjects, error } = await oldSupabaseRestClient
    .from("objects")
    .select();
    if (error) {
    console.log("error getting objects from old bucket");
    throw error;
    }

    for (const objectData of oldObjects) {
    console.log(`moving ${objectData.id}`);
    try {
    const { data, error: downloadObjectError } =
    await oldSupabaseClient.storage
    .from(objectData.bucket_id)
    .download(objectData.name);
    if (downloadObjectError) {
    throw downloadObjectError;
    }

    const { _, error: uploadObjectError } = await newSupabaseClient.storage
    .from(objectData.bucket_id)
    .upload(objectData.name, data, {
    upsert: true,
    contentType: objectData.metadata.mimetype,
    cacheControl: objectData.metadata.cacheControl,
    });
    if (uploadObjectError) {
    throw uploadObjectError;
    }
    } catch (err) {
    console.log("error moving ", objectData);
    console.log(err);
    }
    }
    })();