Skip to content

Commit 8122bb6

Browse files
Merge pull request Backblaze#988 from emnoor-reef/ls-rm-uri
Implement `b2://` URI in `ls` & `rm` in v4
2 parents f0482ca + c38ccc9 commit 8122bb6

File tree

12 files changed

+414
-159
lines changed

12 files changed

+414
-159
lines changed

b2/_internal/_cli/b2args.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,33 @@ def b2_file_uri(value: str) -> B2URIBase:
2929
return b2_uri
3030

3131

32-
B2_URI_ARG_TYPE = wrap_with_argument_type_error(parse_b2_uri)
32+
def b2_uri(value: str) -> B2URI:
33+
uri = parse_b2_uri(value)
34+
if not isinstance(uri, B2URI):
35+
raise ValueError(
36+
f"B2 URI of the form b2://bucket/path/ is required, but {value} was provided"
37+
)
38+
return uri
39+
40+
41+
B2_URI_ARG_TYPE = wrap_with_argument_type_error(b2_uri)
3342
B2_URI_FILE_ARG_TYPE = wrap_with_argument_type_error(b2_file_uri)
3443

3544

45+
def add_b2_uri_argument(parser: argparse.ArgumentParser, name="B2_URI"):
46+
"""
47+
Add a B2 URI pointing to a bucket, optionally with a directory
48+
or a pattern as an argument to the parser.
49+
"""
50+
parser.add_argument(
51+
name,
52+
type=B2_URI_ARG_TYPE,
53+
help="B2 URI pointing to a bucket, directory or a pattern, "
54+
"e.g. b2://yourBucket, b2://yourBucket/file.txt, b2://yourBucket/folder/, "
55+
"b2://yourBucket/*.txt or b2id://fileId",
56+
)
57+
58+
3659
def add_b2_file_argument(parser: argparse.ArgumentParser, name="B2_URI"):
3760
"""
3861
Add a B2 URI pointing to a file as an argument to the parser.

b2/_internal/b2v3/registry.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,47 @@
1010

1111
# ruff: noqa: F405
1212
from b2._internal._b2v4.registry import * # noqa
13+
from .rm import Rm
14+
15+
16+
class Ls(B2URIBucketNFolderNameArgMixin, BaseLs):
17+
"""
18+
{BASELS}
19+
20+
Examples
21+
22+
.. note::
23+
24+
Note the use of quotes, to ensure that special
25+
characters are not expanded by the shell.
26+
27+
28+
List csv and tsv files (in any directory, in the whole bucket):
29+
30+
.. code-block::
31+
32+
{NAME} ls --recursive --withWildcard bucketName "*.[ct]sv"
33+
34+
35+
List all info.txt files from buckets bX, where X is any character:
36+
37+
.. code-block::
38+
39+
{NAME} ls --recursive --withWildcard bucketName "b?/info.txt"
40+
41+
42+
List all pdf files from buckets b0 to b9 (including sub-directories):
43+
44+
.. code-block::
45+
46+
{NAME} ls --recursive --withWildcard bucketName "b[0-9]/*.pdf"
47+
48+
49+
Requires capability:
50+
51+
- **listFiles**
52+
"""
53+
1354

1455
B2.register_subcommand(AuthorizeAccount)
1556
B2.register_subcommand(CancelAllUnfinishedLargeFiles)

b2/_internal/b2v3/rm.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
######################################################################
2+
#
3+
# File: b2/_internal/b2v3/rm.py
4+
#
5+
# Copyright 2023 Backblaze Inc. All Rights Reserved.
6+
#
7+
# License https://www.backblaze.com/using_b2_code.html
8+
#
9+
######################################################################
10+
from __future__ import annotations
11+
12+
from b2._internal._b2v4.registry import B2URIBucketNFolderNameArgMixin, BaseRm
13+
14+
15+
# NOTE: We need to keep v3 Rm in separate file, because we need to import it in
16+
# unit tests without registering any commands.
17+
class Rm(B2URIBucketNFolderNameArgMixin, BaseRm):
18+
"""
19+
{BASERM}
20+
21+
Examples.
22+
23+
.. note::
24+
25+
Note the use of quotes, to ensure that special
26+
characters are not expanded by the shell.
27+
28+
29+
.. note::
30+
31+
Use with caution. Running examples presented below can cause data-loss.
32+
33+
34+
Remove all csv and tsv files (in any directory, in the whole bucket):
35+
36+
.. code-block::
37+
38+
{NAME} rm --recursive --withWildcard bucketName "*.[ct]sv"
39+
40+
41+
Remove all info.txt files from buckets bX, where X is any character:
42+
43+
.. code-block::
44+
45+
{NAME} rm --recursive --withWildcard bucketName "b?/info.txt"
46+
47+
48+
Remove all pdf files from buckets b0 to b9 (including sub-directories):
49+
50+
.. code-block::
51+
52+
{NAME} rm --recursive --withWildcard bucketName "b[0-9]/*.pdf"
53+
54+
55+
Requires capability:
56+
57+
- **listFiles**
58+
- **deleteFiles**
59+
"""

0 commit comments

Comments
 (0)