Skip to content

Question: R2 example #503

@rgreinho

Description

@rgreinho

Would it be possible to add an example to connect to R2 in the documentation please?

I keep getting errors while trying to list the objects in my bucket, so obviously I am doing something wrong, but I cannot figure out what. An example would probably help other people as well 😃

Here is my code:

from __future__ import annotations

import asyncio
import os
from typing import TYPE_CHECKING

import obstore
from obstore.store import (
    S3Store,
)


if TYPE_CHECKING:
    from obstore.store import (
        S3Store,
    )


async def r2_test():
    bucket = os.getenv("BUCKET")
    r2_account_id = os.getenv("R2_ACCOUNT_ID")
    r2_access_key_id = os.getenv("R2_ACCESS_KEY_ID")
    r2_secret_access_key = os.getenv("R2_SECRET_ACCESS_KEY")
    r2_region = os.getenv("R2_REGION")

    r2_store = S3Store(
        bucket=bucket,
        client_options={"connect_timeout": "1h"},
        config={
            "access_key_id": r2_access_key_id,
            "secret_access_key": r2_secret_access_key,
            "endpoint": f"https://{r2_account_id}.r2.cloudflarestorage.com",
            "region": r2_region,
        },
    )
    list_stream = obstore.list(r2_store, "/")
    for batch in list_stream:
        for meta in batch:
            print(f"Name: {meta['path']}, size: {meta['size']}")


if __name__ == "__main__":
    asyncio.run(r2_test())

And here is the error message I get:

obstore.exceptions.GenericError: Generic S3 error: Error performing list request: Error performing GET https://xxxxxxxxxxxxxxxxxxxxx.r2.cloudflarestorage.com/xxxxxxxxxxxxxxxxxxxxxxx?list-type=2 in 218.336667ms - Server returned non-2xx status code: 400 Bad Request: <?xml version="1.0" encoding="UTF-8"?><Error><Code>InvalidArgument</Code><Message>X-Amz-Security-Token</Message></Error>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions