Create an S3 bucket. S3 has no real directories below the bucket level; creating a key prefix requires no operation. This method creates the bucket when the path points at a bucket (or when ``create_parents`` is True and the bucket does not exist yet), and does nothi
(self, path: str, create_parents: bool = True, **kwargs)
| 835 | f.result() |
| 836 | |
| 837 | def mkdir(self, path: str, create_parents: bool = True, **kwargs) -> None: |
| 838 | """Create an S3 bucket. |
| 839 | |
| 840 | S3 has no real directories below the bucket level; creating a key |
| 841 | prefix requires no operation. This method creates the bucket when the |
| 842 | path points at a bucket (or when ``create_parents`` is True and the |
| 843 | bucket does not exist yet), and does nothing for key prefixes under |
| 844 | an existing bucket. |
| 845 | |
| 846 | Bucket lifecycle operations are disabled by default because they are |
| 847 | infrastructure-level changes; pass ``allow_bucket_creation=True`` to |
| 848 | the filesystem constructor to enable bucket creation. |
| 849 | |
| 850 | Args: |
| 851 | path: S3 path (e.g., "s3://bucket" or "s3://bucket/prefix"). |
| 852 | create_parents: If True, create the bucket when it does not exist, |
| 853 | even if the path contains a key prefix. |
| 854 | **kwargs: Additional arguments including: |
| 855 | acl: Canned ACL to apply to the bucket. |
| 856 | region_name: Region to create the bucket in. Defaults to the |
| 857 | client's region. |
| 858 | |
| 859 | Raises: |
| 860 | FileExistsError: If the path is a bucket that already exists. |
| 861 | FileNotFoundError: If the bucket does not exist and |
| 862 | ``create_parents`` is False. |
| 863 | PermissionError: If the bucket would be created but bucket |
| 864 | creation is not enabled on this filesystem instance. |
| 865 | ValueError: If the ACL is invalid or the path is empty. |
| 866 | """ |
| 867 | path = self._strip_protocol(path).rstrip("/") |
| 868 | if not path: |
| 869 | raise ValueError("Cannot create the root directory.") |
| 870 | bucket, key, _ = self.parse_path(path) |
| 871 | if self.exists(bucket): |
| 872 | if not key: |
| 873 | # Requested to create a bucket, but the bucket already exists. |
| 874 | raise FileExistsError(bucket) |
| 875 | # Do nothing as the bucket already exists. |
| 876 | elif not key or create_parents: |
| 877 | if not self.allow_bucket_creation: |
| 878 | raise PermissionError( |
| 879 | "Bucket creation is disabled. " |
| 880 | "Set allow_bucket_creation=True on the filesystem to enable it." |
| 881 | ) |
| 882 | acl = kwargs.pop("acl", "") |
| 883 | if acl and acl not in self.BUCKET_ACLS: |
| 884 | raise ValueError(f"ACL not in {self.BUCKET_ACLS}.") |
| 885 | request: dict[str, Any] = {"Bucket": bucket} |
| 886 | if acl: |
| 887 | request.update({"ACL": acl}) |
| 888 | region_name = kwargs.pop("region_name", None) or self._client.meta.region_name |
| 889 | if region_name and region_name != "us-east-1": |
| 890 | # us-east-1 does not accept a location constraint. |
| 891 | request.update({"CreateBucketConfiguration": {"LocationConstraint": region_name}}) |
| 892 | |
| 893 | _logger.debug(f"Create bucket: s3://{bucket}") |
| 894 | try: |