Backblaze cors 无法与本地主机一起使用

问题描述 投票:0回答:1

我正在关注 这个 github 存储库这个答案

规则.json

[
    {
        "corsRuleName": "downloadFromAnyOriginWithUpload",
        "allowedOrigins": [
            "*"
        ],
        "allowedHeaders": [
            "authorization",
            "content-type",
            "x-bz-file-name",
            "x-bz-content-sha1"
        ],
        "allowedOperations": [
            "b2_download_file_by_id",
            "b2_download_file_by_name",
            "b2_upload_file",
            "b2_upload_part"
        ],
        "maxAgeSeconds": 3600
    }
]
$ ./b2-windows.exe update-bucket --cors-rules "$(cat rules.json)" private-bucket2
{
    "accountId": "5c83d2c9fb2a",
    "bucketId": "d51c58c36d022cb98ffb021a",
    "bucketInfo": {},
    "bucketName": "private-bucket2",
    "bucketType": "allPrivate",
    "corsRules": [
        {
            "allowedHeaders": [
                "authorization",
                "content-type",
                "x-bz-file-name",
                "x-bz-content-sha1"
            ],
            "allowedOperations": [
                "b2_download_file_by_id",
                "b2_upload_part",
                "b2_upload_file",
                "b2_download_file_by_name"
            ],
            "allowedOrigins": [
                "*"
            ],
            "corsRuleName": "downloadFromAnyOriginWithUpload",
            "exposeHeaders": null,
            "maxAgeSeconds": 3600
        }
    ],
    "defaultRetention": {
        "mode": null
    },
    "defaultServerSideEncryption": {
        "mode": "none"
    },
    "isFileLockEnabled": false,
    "lifecycleRules": [],
    "options": [
        "s3"
    ],
    "replication": {
        "asReplicationDestination": null,
        "asReplicationSource": null
    },
    "revision": 16
}

在我的本地主机前端我有:

// Code I run on the frontend:
const s3 = new S3Client({
    endpoint: 'https://s3.us-east-005.backblazeb2.com',
    region: 'us-east-005',
    credentials: {
        accessKeyId: "5c83d2c9fb2a",
        secretAccessKey: "0058d9a7de2103531f2f28e402f4bd5e7ad68d054c"
    }
});

const x = await s3.send(new PutObjectCommand({
    Bucket: "private-bucket2",
    Key: "my-filename.jpg",
    Body: blob
}));

enter image description here


我也尝试过 backblaze-b2 包

// Code I run on the frontend:
import B2 from "backblaze-b2";
const b2 = new B2({
    applicationKeyId: '5c83d2c9fb2a', // or accountI: 'accountId'
    applicationKey: '0058d9a7de2103531f2f28e402f4bd5e7ad68d054c' // or masterApplicationKey
});
await b2.authorize();

也不顺利: enter image description here


enter image description here enter image description here

实际要求: enter image description here 飞行前请求: enter image description here

cors localhost backblaze
1个回答
0
投票

我明白了。

rules.json

[
    {
        "corsRuleName": "downloadFromAnyOriginWithUpload",
        "allowedOrigins": [
            "*"
        ],
        "allowedHeaders": [
            "authorization",
            "content-type",
            "x-bz-file-name",
            "x-bz-content-sha1"
        ],
        "allowedOperations": [
            "b2_download_file_by_id",
            "b2_download_file_by_name",
            "b2_upload_file",
            "b2_upload_part",
            "s3_head",
            "s3_get",
            "s3_put"
        ],
        "maxAgeSeconds": 3600
    }
]

运行这个

./b2-windows update-bucket --cors-rules "$(cat rules.json)" profile-bucket

通过运行确认它有效:

./b2-windows get-bucket profile-bucket

创建应用程序密钥并记下 id 和密钥

enter image description here

// Insert keyId and appKey and the name of the bucket:
const key = {
    keyId: "00xxxxxxxx9fb2030007",
    appKey: "K00xxxxxxxbwuxxxxmsEyWbD/F4AxU",
    bucket: "my-bucket"
}


const s3Client = new S3Client({
        region,
        endpoint,
        credentials: {
            accessKeyId: key.keyId,
            secretAccessKey: key.appKey
        }
});

const expiresIn = 7 * 24 * 60 * 60; // 3600
const command = new PutObjectCommand({ Bucket: key.bucket, Key: filename });
const signedUrl = await getSignedUrl(s3Client, command, { expiresIn })
await axios.put(signedUrl, file);
© www.soinside.com 2019 - 2024. All rights reserved.