I was following this github repo and this answer.
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"
],
"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
}
In my localhost frontend I have:
// 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
}));
I also tried with the backblaze-b2 package
// 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();
I figured it out.
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
}
]
Run this
./b2-windows update-bucket --cors-rules "$(cat rules.json)" profile-bucket
Confirm that it worked by running:
./b2-windows get-bucket profile-bucket
Create an application key and take note of the id and key
// 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);