Skip to content

Commit

Permalink
deprecated download-url-with-auth and replaced with file url
Browse files Browse the repository at this point in the history
  • Loading branch information
adal-chiriliuc-reef committed Apr 29, 2024
1 parent b17e93c commit d635f36
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
38 changes: 36 additions & 2 deletions b2/_internal/console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ def _run(self, args):
return 0


class GetDownloadUrlWithAuth(Command):
class GetDownloadUrlWithAuthBase(Command):
"""
Prints a URL to download the given file. The URL includes an authorization
token that allows downloads from the given bucket for files whose names
Expand Down Expand Up @@ -2745,11 +2745,40 @@ class FileUrlBase(Command):
"""
Prints an URL that can be used to download the given file, if
it is public.
If it is private, you can use --with-auth to include an authorization
token in the URL that allows downloads from the given bucket for files
whose names start with the given file name.
The URL will work for the given file, but is not specific to that file. Files
with longer names that start with the give file name can also be downloaded
with the same auth token.
The token is valid for the duration specified, which defaults
to 86400 seconds (one day).
Requires capability:
- **shareFiles** (if using --with-auth)
"""

@classmethod
def _setup_parser(cls, parser):
add_normalized_argument(parser, '--with-auth', action='store_true')
parser.add_argument('--duration', type=int, default=86400)
super()._setup_parser(parser)

def _run(self, args):
b2_uri = self.get_b2_uri_from_arg(args)
self._print(self.api.get_download_url_by_uri(b2_uri))
url = self.api.get_download_url_by_uri(b2_uri)
if args.with_auth:
bucket = self.api.get_bucket_by_name(b2_uri.bucket_name)
auth_token = bucket.get_download_authorization(
file_name_prefix=b2_uri.path, valid_duration_in_seconds=args.duration
)
url += '?Authorization=' + auth_token
self._print(url)
return 0


Expand Down Expand Up @@ -5078,6 +5107,11 @@ class UpdateFileRetention(CmdReplacedByMixin, UpdateFileRetentionBase):
replaced_by_cmd = (File, FileUpdate)


class GetDownloadUrlWithAuth(CmdReplacedByMixin, GetDownloadUrlWithAuthBase):
__doc__ = GetDownloadUrlWithAuthBase.__doc__
replaced_by_cmd = (File, FileUrl)


class ConsoleTool:
"""
Implements the commands available in the B2 command-line tool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecated `get-download-url-with-auth`, use `file url` instead.
12 changes: 12 additions & 0 deletions test/unit/test_console_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,12 @@ def test_get_download_auth_url(self):
self._run_command(
['get-download-url-with-auth', '--duration', '12345', 'my-bucket', 'my-file'],
'http://download.example.com/file/my-bucket/my-file?Authorization=fake_download_auth_token_bucket_0_my-file_12345\n',
'WARNING: `get-download-url-with-auth` command is deprecated. Use `file url` instead.\n',
0
)
self._run_command(
['file', 'url', '--with-auth', '--duration', '12345', 'b2://my-bucket/my-file'],
'http://download.example.com/file/my-bucket/my-file?Authorization=fake_download_auth_token_bucket_0_my-file_12345\n',
'', 0
)

Expand All @@ -1647,6 +1653,12 @@ def test_get_download_auth_url_with_encoding(self):
self._run_command(
['get-download-url-with-auth', '--duration', '12345', 'my-bucket', '\u81ea'],
'http://download.example.com/file/my-bucket/%E8%87%AA?Authorization=fake_download_auth_token_bucket_0_%E8%87%AA_12345\n',
'WARNING: `get-download-url-with-auth` command is deprecated. Use `file url` instead.\n',
0
)
self._run_command(
['file', 'url', '--with-auth', '--duration', '12345', 'b2://my-bucket/\u81ea'],
'http://download.example.com/file/my-bucket/%E8%87%AA?Authorization=fake_download_auth_token_bucket_0_%E8%87%AA_12345\n',
'', 0
)

Expand Down

0 comments on commit d635f36

Please sign in to comment.