base#

The base constructor of S3Path object.

class s3pathlib.core.base.BaseS3Path(*args: str | S3Path)[source]#

Similar to pathlib.Path. An objective oriented programming interface for AWS S3 object or logical directory.

You can use this class in different way.

  1. pure s3 object / directory path manipulation without actually

    talking to AWS API.

  2. get metadata of an object, count objects, get statistics information

    of a directory

  3. enhanced s3 API that do: upload_file,

    upload_dir, copy a file or directory, move a file or directory, delete a file or directory, iter_objects from a prefix.

Constructor

The S3Path itself is a constructor. It takes str and other relative S3Path as arguments.

  1. The first argument defines the S3 bucket

  2. The rest of arguments defines the path parts of the S3 key

  3. The final argument defines the type whether is a file (object) or

    a directory

First let’s create a S3 object path from string:

# first argument becomes the bucket
>>> s3path = S3Path("bucket", "folder", "file.txt")
# print S3Path object gives you info in S3 URI format
>>> s3path
S3Path('s3://bucket/folder/file.txt')

# If the last argument has a trailing slash ("/"), it indicates that
# it refers to a directory. Otherwise, it is assumed to be a file.
>>> s3path.is_file()
True
>>> s3path.is_dir()
False

# "/" separator will be automatically handled
>>> S3Path("bucket", "folder/file.txt")
S3Path('s3://bucket/folder/file.txt')

>>> S3Path("bucket/folder/file.txt")
S3Path('s3://bucket/folder/file.txt')

Then let’s create a S3 directory path:

>>> s3path= S3Path("bucket/folder/")
>>> s3path
S3Path('s3://bucket/folder/')

# last argument defines that it is a directory
>>> s3path.is_dir()
True
>>> s3path.is_file()
False

You can also create it from S3 URI or ARN:

>>> S3Path("s3://bucket/folder/file.txt")
S3Path('s3://bucket/folder/file.txt')
>>> S3Path("arn:aws:s3:::bucket/folder/file.txt"),
S3Path('s3://bucket/folder/file.txt')

New in version 1.0.1.

Changed in version 2.0.1: You can create S3Path from s3 uri or arn directly.

parts[source]#

Provides sequence-like access to the components in the filesystem path. It doesn’t include the bucket, because bucket is considered as “drive”.

New in version 1.0.1.