File size: 1,880 Bytes
105b369
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from pathlib import Path
from typing import Any, Optional

from pydantic import Field

from phi.aws.api_client import AwsApiClient
from phi.aws.resource.base import AwsResource
from phi.utils.log import logger


class S3Object(AwsResource):
    """
    Reference:
    - https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/object/index.html
    """

    resource_type: str = "s3"
    service_name: str = "s3"

    # The Object’s bucket_name identifier. This must be set.
    bucket_name: str
    # The Object’s key identifier. This must be set.
    name: str = Field(..., alias="key")

    @property
    def uri(self) -> str:
        """Returns the URI of the s3.Object

        Returns:
            str: The URI of the s3.Object
        """
        return f"s3://{self.bucket_name}/{self.name}"

    def get_resource(self, aws_client: Optional[AwsApiClient] = None) -> Any:
        """Returns the s3.Object

        Args:
            aws_client: The AwsApiClient for the current cluster

        Returns:
            The s3.Object
        """
        client: AwsApiClient = aws_client or self.get_aws_client()
        service_resource = self.get_service_resource(client)
        return service_resource.Object(
            bucket_name=self.bucket_name,
            key=self.name,
        )

    def download(self, path: Path, aws_client: Optional[AwsApiClient] = None) -> None:
        """Downloads the s3.Object to the specified path

        Args:
            path: The path to download the s3.Object to
            aws_client: The AwsApiClient for the current cluster
        """
        logger.info(f"Downloading {self.uri} to {path}")
        object_resource = self.get_resource(aws_client=aws_client)
        path.parent.mkdir(parents=True, exist_ok=True)
        with path.open(mode="wb") as f:
            object_resource.download_fileobj(f)