모토의 소스 코드에 대해 자세히 알아보기

6658 단어 motopythonaws
모토 버전: 3.1.x

아마존 S3



버킷



用FakeBucket은 새로운 버킷입니다.

https://github.com/spulec/moto/blob/3.1.6/moto/s3/models.py#L847

만들기 create_bucket



每個bucket都會對應一個FakeBucket 인스턴스.

並且用buckets(dict object)는 버킷의 인스턴스입니다.

def create_bucket(self, bucket_name, region_name):
    ...
    new_bucket = FakeBucket(name=bucket_name, region_name=region_name)

    self.buckets[bucket_name] = new_bucket
    return new_bucket


如何實作S3物件的多版本特性


_VersionedKeyStore 實現dict一個key可以儲存多個value,但是get method只會回傳最新的value.

https://github.com/spulec/moto/blob/3.1.6/moto/s3/utils.py#L102

물체



用FakeKey는 새로운 개체입니다.

https://github.com/spulec/moto/blob/3.1.6/moto/s3/models.py#L102

如何實作 put_object



https://github.com/spulec/moto/blob/3.1.6/moto/s3/models.py#L1608

上傳的 物件 對應至參數 value .

如何實作 delete_object



dictpop 模擬刪除物件的行為.

https://github.com/spulec/moto/blob/3.1.6/moto/s3/models.py#L1989

moto如何避免吃光記憶體 - SpooledTemporaryFile



由SpooledTemporaryFile instance負責處理Python object的儲存方式.當物件大小超過 max_size 就會寫入硬碟.

https://docs.python.org/3/library/tempfile.html#tempfile.SpooledTemporaryFile

This class operates exactly as TemporaryFile() does, except that data is spooled in memory until the file size exceeds max_size, or until the file’s fileno() method is called, at which point the contents are written to disk and operation proceeds as with TemporaryFile().




self._value_buffer = tempfile.SpooledTemporaryFile(self._max_buffer_size)

...

@property
def value(self):
    self.lock.acquire()
    self._value_buffer.seek(0)
    r = self._value_buffer.read()
    r = copy.copy(r)
    self.lock.release()
    return r

...

@value.setter
def value(self, new_value):
    self._value_buffer.seek(0)
    self._value_buffer.truncate()

    # Hack for working around moto's own unit tests; this probably won't
    # actually get hit in normal use.
    if isinstance(new_value, str):
        new_value = new_value.encode(DEFAULT_TEXT_ENCODING)
    self._value_buffer.write(new_value)
    self.contentsize = len(new_value)

좋은 웹페이지 즐겨찾기