99######################################################################
1010
1111import logging
12+
1213from typing import Optional , Tuple
1314
1415from .encryption .setting import EncryptionSetting , EncryptionSettingFactory
1516from .encryption .types import EncryptionMode
16- from .exception import BucketIdNotFound , CopySourceTooBig , FileNotPresent , FileOrBucketNotFound , UnexpectedCloudBehaviour , UnrecognizedBucketType
17+ from .exception import (
18+ BucketIdNotFound ,
19+ CopySourceTooBig ,
20+ FileNotPresent ,
21+ FileOrBucketNotFound ,
22+ UnexpectedCloudBehaviour ,
23+ UnrecognizedBucketType ,
24+ )
1725from .file_lock import (
26+ UNKNOWN_BUCKET_RETENTION ,
1827 BucketRetentionSetting ,
1928 FileLockConfiguration ,
2029 FileRetentionSetting ,
21- UNKNOWN_BUCKET_RETENTION ,
2230 LegalHold ,
2331)
2432from .file_version import DownloadVersion , FileVersion
2533from .progress import AbstractProgressListener , DoNothingProgressListener
34+ from .replication .setting import ReplicationConfiguration , ReplicationConfigurationFactory
2635from .transfer .emerge .executor import AUTO_CONTENT_TYPE
2736from .transfer .emerge .write_intent import WriteIntent
2837from .transfer .inbound .downloaded_file import DownloadedFile
2938from .transfer .outbound .copy_source import CopySource
3039from .transfer .outbound .upload_source import UploadSourceBytes , UploadSourceLocalFile
31- from .utils import B2TraceMeta , disable_trace , limit_trace_arguments
32- from .utils import b2_url_encode , validate_b2_file_name
40+ from .utils import (
41+ B2TraceMeta ,
42+ b2_url_encode ,
43+ disable_trace ,
44+ limit_trace_arguments ,
45+ validate_b2_file_name ,
46+ )
3347
3448logger = logging .getLogger (__name__ )
3549
@@ -58,6 +72,7 @@ def __init__(
5872 ),
5973 default_retention : BucketRetentionSetting = UNKNOWN_BUCKET_RETENTION ,
6074 is_file_lock_enabled : Optional [bool ] = None ,
75+ replication : Optional [ReplicationConfiguration ] = None ,
6176 ):
6277 """
6378 :param b2sdk.v2.B2Api api: an API object
@@ -73,6 +88,7 @@ def __init__(
7388 :param b2sdk.v2.EncryptionSetting default_server_side_encryption: default server side encryption settings
7489 :param b2sdk.v2.BucketRetentionSetting default_retention: default retention setting
7590 :param bool is_file_lock_enabled: whether file locking is enabled or not
91+ :param b2sdk.v2.ReplicationConfiguration replication: replication rules for the bucket
7692 """
7793 self .api = api
7894 self .id_ = id_
@@ -87,6 +103,7 @@ def __init__(
87103 self .default_server_side_encryption = default_server_side_encryption
88104 self .default_retention = default_retention
89105 self .is_file_lock_enabled = is_file_lock_enabled
106+ self .replication = replication
90107
91108 def get_fresh_state (self ) -> 'Bucket' :
92109 """
@@ -98,15 +115,15 @@ def get_fresh_state(self) -> 'Bucket':
98115 raise BucketIdNotFound (self .id_ )
99116 return buckets_found [0 ]
100117
101- def get_id (self ):
118+ def get_id (self ) -> str :
102119 """
103120 Return bucket ID.
104121
105122 :rtype: str
106123 """
107124 return self .id_
108125
109- def set_info (self , new_bucket_info , if_revision_is = None ):
126+ def set_info (self , new_bucket_info , if_revision_is = None ) -> 'Bucket' :
110127 """
111128 Update bucket info.
112129
@@ -115,7 +132,7 @@ def set_info(self, new_bucket_info, if_revision_is=None):
115132 """
116133 return self .update (bucket_info = new_bucket_info , if_revision_is = if_revision_is )
117134
118- def set_type (self , bucket_type ):
135+ def set_type (self , bucket_type ) -> 'Bucket' :
119136 """
120137 Update bucket type.
121138
@@ -132,7 +149,8 @@ def update(
132149 if_revision_is : Optional [int ] = None ,
133150 default_server_side_encryption : Optional [EncryptionSetting ] = None ,
134151 default_retention : Optional [BucketRetentionSetting ] = None ,
135- ):
152+ replication : Optional [ReplicationConfiguration ] = None ,
153+ ) -> 'Bucket' :
136154 """
137155 Update various bucket parameters.
138156
@@ -143,6 +161,7 @@ def update(
143161 :param if_revision_is: revision number, update the info **only if** *revision* equals to *if_revision_is*
144162 :param default_server_side_encryption: default server side encryption settings (``None`` if unknown)
145163 :param default_retention: bucket default retention setting
164+ :param replication: replication rules for the bucket;
146165 """
147166 account_id = self .api .account_info .get_account_id ()
148167 return self .api .BUCKET_FACTORY_CLASS .from_api_bucket_dict (
@@ -157,6 +176,7 @@ def update(
157176 if_revision_is = if_revision_is ,
158177 default_server_side_encryption = default_server_side_encryption ,
159178 default_retention = default_retention ,
179+ replication = replication ,
160180 )
161181 )
162182
@@ -936,6 +956,7 @@ def as_dict(self):
936956 result ['defaultServerSideEncryption' ] = self .default_server_side_encryption .as_dict ()
937957 result ['isFileLockEnabled' ] = self .is_file_lock_enabled
938958 result ['defaultRetention' ] = self .default_retention .as_dict ()
959+ result ['replication' ] = self .replication and self .replication .as_dict ()
939960
940961 return result
941962
@@ -967,32 +988,64 @@ def from_api_bucket_dict(cls, api, bucket_dict):
967988
968989 .. code-block:: python
969990
970- {
971- "bucketType": "allPrivate",
972- "bucketId": "a4ba6a39d8b6b5fd561f0010",
973- "bucketName": "zsdfrtsazsdfafr",
974- "accountId": "4aa9865d6f00",
975- "bucketInfo": {},
976- "options": [],
977- "revision": 1,
978- "defaultServerSideEncryption": {
979- "isClientAuthorizedToRead" : true,
980- "value": {
981- "algorithm" : "AES256",
982- "mode" : "SSE-B2"
983- }
984- },
985- "fileLockConfiguration": {
986- "isClientAuthorizedToRead": true,
987- "value": {
988- "defaultRetention": {
989- "mode": null,
990- "period": null
991+ {
992+ "bucketType": "allPrivate",
993+ "bucketId": "a4ba6a39d8b6b5fd561f0010",
994+ "bucketName": "zsdfrtsazsdfafr",
995+ "accountId": "4aa9865d6f00",
996+ "bucketInfo": {},
997+ "options": [],
998+ "revision": 1,
999+ "defaultServerSideEncryption": {
1000+ "isClientAuthorizedToRead" : true,
1001+ "value": {
1002+ "algorithm" : "AES256",
1003+ "mode" : "SSE-B2"
1004+ }
1005+ },
1006+ "fileLockConfiguration": {
1007+ "isClientAuthorizedToRead": true,
1008+ "value": {
1009+ "defaultRetention": {
1010+ "mode": null,
1011+ "period": null
1012+ },
1013+ "isFileLockEnabled": false
1014+ }
1015+ },
1016+ "replicationConfiguration": {
1017+ "clientIsAllowedToRead": true,
1018+ "value": {
1019+ "asReplicationSource": {
1020+ "replicationRules": [
1021+ {
1022+ "destinationBucketId": "c5f35d53a90a7ea284fb0719",
1023+ "fileNamePrefix": "",
1024+ "includeExistingFiles": True,
1025+ "isEnabled": true,
1026+ "priority": 1,
1027+ "replicationRuleName": "replication-us-west"
1028+ },
1029+ {
1030+ "destinationBucketId": "55f34d53a96a7ea284fb0719",
1031+ "fileNamePrefix": "",
1032+ "includeExistingFiles": True,
1033+ "isEnabled": true,
1034+ "priority": 2,
1035+ "replicationRuleName": "replication-us-west-2"
1036+ }
1037+ ],
1038+ "sourceApplicationKeyId": "10053d55ae26b790000000006"
9911039 },
992- "isFileLockEnabled": false
1040+ "asReplicationDestination": {
1041+ "sourceToDestinationKeyMapping": {
1042+ "10053d55ae26b790000000045": "10053d55ae26b790000000004",
1043+ "10053d55ae26b790000000046": "10053d55ae26b790030000004"
1044+ }
1045+ }
9931046 }
994- }
995- }
1047+ }
1048+ }
9961049
9971050 into a Bucket object.
9981051
@@ -1016,6 +1069,7 @@ def from_api_bucket_dict(cls, api, bucket_dict):
10161069 raise UnexpectedCloudBehaviour ('server did not provide `defaultServerSideEncryption`' )
10171070 default_server_side_encryption = EncryptionSettingFactory .from_bucket_dict (bucket_dict )
10181071 file_lock_configuration = FileLockConfiguration .from_bucket_dict (bucket_dict )
1072+ replication = ReplicationConfigurationFactory .from_bucket_dict (bucket_dict ).value
10191073 return cls .BUCKET_CLASS (
10201074 api ,
10211075 bucket_id ,
@@ -1030,4 +1084,5 @@ def from_api_bucket_dict(cls, api, bucket_dict):
10301084 default_server_side_encryption ,
10311085 file_lock_configuration .default_retention ,
10321086 file_lock_configuration .is_file_lock_enabled ,
1087+ replication ,
10331088 )
0 commit comments