From d635f36403cd965d48fc3d273b56d28682c7131d Mon Sep 17 00:00:00 2001 From: Adal Chiriliuc Date: Mon, 29 Apr 2024 18:57:07 +0300 Subject: [PATCH] deprecated download-url-with-auth and replaced with file url --- b2/_internal/console_tool.py | 38 ++++++++++++++++++- ...d-get-download-url-with-auth.deprecated.md | 1 + test/unit/test_console_tool.py | 12 ++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 changelog.d/+command-get-download-url-with-auth.deprecated.md diff --git a/b2/_internal/console_tool.py b/b2/_internal/console_tool.py index 0221b2d1..7447958c 100644 --- a/b2/_internal/console_tool.py +++ b/b2/_internal/console_tool.py @@ -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 @@ -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 @@ -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 diff --git a/changelog.d/+command-get-download-url-with-auth.deprecated.md b/changelog.d/+command-get-download-url-with-auth.deprecated.md new file mode 100644 index 00000000..9150ada1 --- /dev/null +++ b/changelog.d/+command-get-download-url-with-auth.deprecated.md @@ -0,0 +1 @@ +Deprecated `get-download-url-with-auth`, use `file url` instead. \ No newline at end of file diff --git a/test/unit/test_console_tool.py b/test/unit/test_console_tool.py index e0dc837f..cb3645df 100644 --- a/test/unit/test_console_tool.py +++ b/test/unit/test_console_tool.py @@ -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 ) @@ -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 )