summaryrefslogtreecommitdiff
path: root/paste/s3.py
blob: a9b2b21389fe48653caf03b83c2499483d42e9d7 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from datetime import datetime, UTC
from typing import Dict
from dataclasses import dataclass
from bozo4 import s3v4_sign_request, s3v4_datetime_string


@dataclass
class Request:
    url: str
    headers: Dict[str, str]


@dataclass
class Bucket:
    endpoint: str
    region: str
    bucket: str
    access_key: str
    secret_key: str

    def get(self, key: str) -> Request:
        now = datetime.now(UTC)

        headers = {
            "Host": self.endpoint,
            "X-Amz-Date": s3v4_datetime_string(now),
            "X-Amz-Content-SHA256": "UNSIGNED-PAYLOAD",
        }

        auth = s3v4_sign_request(
            endpoint=self.endpoint,
            region=self.region,
            access_key=self.access_key,
            secret_key=self.secret_key,
            request_method="GET",
            date=now,
            payload_hash="UNSIGNED-PAYLOAD",
            uri=f"/{self.bucket}/{key}",
            parameters={},
            headers=headers,
            service="s3",
        )

        headers["Authorization"] = auth

        return Request(f"https://{self.endpoint}/{self.bucket}/{key}", headers)

    def put(
        self, key: str, content_length: int, content_type: str, payload_hash: str
    ) -> Request:
        now = datetime.now(UTC)

        headers = {
            "Host": self.endpoint,
            "X-Amz-Date": s3v4_datetime_string(now),
            "X-Amz-Content-SHA256": payload_hash,
            "Content-Length": str(content_length),
            "Content-Type": content_type,
        }

        auth = s3v4_sign_request(
            endpoint=self.endpoint,
            region=self.region,
            access_key=self.access_key,
            secret_key=self.secret_key,
            request_method="PUT",
            date=now,
            payload_hash=payload_hash,
            uri=f"/{self.bucket}/{key}",
            parameters={},
            headers=headers,
            service="s3",
        )

        headers["Authorization"] = auth

        return Request(f"https://{self.endpoint}/{self.bucket}/{key}", headers)

    def delete(self, key: str) -> Request:
        now = datetime.now(UTC)

        headers = {
            "Host": self.endpoint,
            "X-Amz-Date": s3v4_datetime_string(now),
            "X-Amz-Content-SHA256": "UNSIGNED-PAYLOAD",
        }

        auth = s3v4_sign_request(
            endpoint=self.endpoint,
            region=self.region,
            access_key=self.access_key,
            secret_key=self.secret_key,
            request_method="DELETE",
            date=now,
            payload_hash="UNSIGNED-PAYLOAD",
            uri=f"/{self.bucket}/{key}",
            parameters={},
            headers=headers,
            service="s3",
        )

        headers["Authorization"] = auth

        return Request(f"https://{self.endpoint}/{self.bucket}/{key}", headers)