| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- // Migrate OSS data from Hangzhou to Shanghai, update env & DB references
- const OSS = require('ali-oss');
- const mysql = require('mysql2/promise');
- const fs = require('fs');
- const path = require('path');
- const OLD_ACCESS_KEY = 'LTAI5tBn4G5HMo8PqMdNsqHd';
- const OLD_SECRET = 'f0R5nOQf6E1lIGESENKiK8cNzFl8wd';
- const OLD_BUCKET = 'aaaa33dfsf32rfsf';
- const OLD_REGION = 'oss-cn-hangzhou';
- const NEW_BUCKET = 'aaaa33dfsf32rfsf';
- const NEW_REGION = 'oss-cn-shanghai';
- const DB_URL = 'mysql://root:123456@127.0.0.1:3306/audio_book';
- // Note: bucket names are globally unique. If we can't use same name,
- // we'll use a different name and update all references.
- async function main() {
- // Connect to old bucket using public endpoint
- const oldClient = new OSS({
- region: OLD_REGION,
- accessKeyId: OLD_ACCESS_KEY,
- accessKeySecret: OLD_SECRET,
- bucket: OLD_BUCKET,
- secure: true,
- });
- // First, try to create bucket in Shanghai with same name
- // Use public endpoint for bucket creation
- const newClient = new OSS({
- region: NEW_REGION,
- accessKeyId: OLD_ACCESS_KEY,
- accessKeySecret: OLD_SECRET,
- bucket: NEW_BUCKET,
- secure: true,
- });
- try {
- // Check if bucket already exists in Shanghai
- await newClient.getBucketInfo();
- console.log('[OK] Shanghai bucket already exists:', NEW_BUCKET);
- } catch (e) {
- if (e.code === 'NoSuchBucket' || e.code === 'AccessDenied') {
- console.log('[INFO] Shanghai bucket does not exist, creating...');
- try {
- await newClient.putBucket();
- console.log('[OK] Created Shanghai bucket:', NEW_BUCKET);
- // Set public-read ACL
- await newClient.putBucketACL(NEW_BUCKET, 'public-read');
- console.log('[OK] Set public-read ACL');
- } catch (createErr) {
- console.error('[FAIL] Cannot create bucket:', createErr.message);
- console.log('[INFO] Bucket name may already exist in another region. Trying with suffix...');
- // Try with -sh suffix
- process.exit(1);
- }
- } else {
- console.error('[FAIL] Bucket access error:', e.message);
- process.exit(1);
- }
- }
- // For now, test connectivity with same public endpoint first
- console.log('[TEST] Testing new bucket connectivity...');
- try {
- await newClient.list({ 'max-keys': 1 });
- console.log('[OK] New bucket accessible');
- } catch (e) {
- console.error('[FAIL] New bucket not accessible:', e.message);
- }
- }
- main().catch(e => { console.error(e); process.exit(1); });
|