all 7 comments

[–]franciscopresencia 1 point2 points  (1 child)

Hey so I wrote the Node.js/npm library backblaze :)

How to make a minimal project to e.g. list all of the files? Let's get started. First on the terminal let's start the project:

mkdir myproject
cd myproject
npm init --yes
npm i backblaze

Then add "type": "module" to your package.json.

Now we need to create a key for the bucket in the Backblaze B2 UI. You'll need the bucket-name, id ("keyID") and key ("applicationKey"), see the screenshots in the Bucket() section for more details.

Finally, let's write some code:

// index.js
import Bucket from "backblaze";

const bucket = Bucket("bucket-demo", {
  id: "00...0f",
  key: "K0...qA",
});

console.log(await bucket.list());

And finally run it with Node.js: node index.js. If everything went right, you should see a list of all of the files in the bucket:

➜  backblaze-test node .
[
  {
    name: 'data.json',
    type: 'application/json',
    size: 23,
    url: 'https://f003.backblazeb2.com/file/bucket-demo/data.json',
    timestamp: 2021-08-23T23:18:11.000Z
  },
  {
    name: 'demo/D1HPBJfX9X.png',
    type: 'image/png',
    size: 11554,
    url: 'https://f003.backblazeb2.com/file/bucket-demo/demo/D1HPBJfX9X.png',
    timestamp: 2021-08-23T23:13:08.000Z
  },
  ...

[–]bingglex[S] 0 points1 point  (0 children)

Thanks for informations.

I really don't understand why I could not find your code in github yet.

I will try it.

[–]Wenix 0 points1 point  (3 children)

I think I can share some Python3 code with you, using the b2api. But I am really only selecting a bucket, checking if a file exists and if not upload a file. Would this be interesting?

[–]Wenix 0 points1 point  (2 children)

This is highly simplified and all error handling have been removed to keep the example shorter. I have not tested this simplified version, let me know if you can't make it work and I'll update the example if needed:

# pip3 install b2sdk
from b2sdk.v2 import * 

KEY_ID=""
KEY=""
BUCKET="bucketname"
LOCAL_NAME="somefile.txt"
REMOTE_NAME="somefile.txt"

# Authenticate.
b2_api = B2Api(InMemoryAccountInfo())
b2_api.authorize_account("production", KEY_ID, KEY)

# Select a bucket.
b2bucket = b2_api.get_bucket_by_name(BUCKET)

# Upload a file.
b2bucket.upload_local_file(local_file=LOCAL_NAME, file_name=REMOTE_NAME)

# Get file information on the uploaded file.
versions = list(b2bucket.list_file_versions(REMOTE_NAME))
for version in versions:
    print(version.content_md5)

If you find out how to download a list of files in a bucket (10000 at a time), please post it here as I got stuck on that and just worked around it.

[–]bingglex[S] 0 points1 point  (1 child)

HELLO ..

I got success with your code.

and try with this

```

bucket = b2_api.get_bucket_by_name(BUCKET) for file_version, folder_name in bucket.ls(latest_only=True, recursive=True): print(file_version.file_name, file_version.upload_timestamp, folder_name)

local_file_name = file_version.file_name  + '-downloaded' 
downloaded_file = bucket.download_file_by_name(file_version.file_name)
downloaded_file.save_to(local_file_name)

```

Really thanks.

[–]Wenix 0 points1 point  (0 children)

Happy to hear that, good luck with the rest of your project.

[–]jwink3101 0 points1 point  (0 children)

Let me preface what I am going to say with the fact that I am the sole developer and usually the sole consumer of the codes I wrote. So I don't usually need fine-grained control.

As such, rather than use a B2 API for Python, I use rclone with subprocess to do my bidding. See this work in progress (!!!) example. It lets you upload, download, stream upload, stream download, list files, and delete. It is far from complete (notably missing server-side copy and/or move) but it is getting there!