コード例 #1
0
    def _mkdir(self, mode=0x777):
        if not self._exists(to_dir_key(self.parent.key)):
            raise FileNotFoundError

        if self.exists():
            raise OSError
        self._client.put_object(Bucket=self.bucket, Key=to_dir_key(self.key))
コード例 #2
0
    def _mkdir(self, mode=0x777):
        if not self._exists(to_dir_key(self.parent.key)):
            raise FileNotFoundError

        if self.exists():
            raise OSError
        gcs.open(self.bucket,
                 to_dir_key(self.key),
                 WRITE_BINARY,
                 client=self._client).close()
コード例 #3
0
 def is_dir(self):
     try:
         dir_key = to_dir_key(self.key)
         res = self._client.head_object(Bucket=self.bucket, Key=dir_key)
         return res["ContentLength"] == 0
     except ClientError:
         return False
コード例 #4
0
 def iterdir(self):
     prefix = to_dir_key(self.key) if self.key != "" else self.key
     blobs = self._client.list_blobs(self.bucket,
                                     prefix=prefix,
                                     delimiter="/")
     for blob in blobs:
         if blob.name in {".", "..", prefix}:
             continue
         yield GCSPath(f"{self.anchor}/{blob.name}")
     for blob_prefix in blobs.prefixes:
         yield GCSPath(f"{self.anchor}/{blob_prefix}")
コード例 #5
0
    def touch(self, mode=0x666, exist_ok=True):
        if not self._exists(to_dir_key(self.parent.key)):
            raise FileNotFoundError

        if exist_ok:
            try:
                bucket = self._client.get_bucket(self.bucket)
                bucket.copy_blob(bucket.get_blob(self.key), bucket, self.key)
            except NotFound:
                pass
            else:
                return
        if not exist_ok and self.exists():
            raise FileExistsError
        gcs.open(self.bucket, self.key, WRITE_BINARY,
                 client=self._client).close()
コード例 #6
0
    def touch(self, mode=0x666, exist_ok=True):
        if not self._exists(to_dir_key(self.parent.key)):
            raise FileNotFoundError

        if exist_ok:
            try:
                source = f"{self.bucket}/{self.key}"
                self._client.copy_object(CopySource=source,
                                         Bucket=self.bucket,
                                         Key=self.key)
            except ClientError:
                pass
            else:
                return
        if not exist_ok and self.exists():
            raise FileExistsError
        self._client.put_object(Bucket=self.bucket, Key=self.key)
コード例 #7
0
 def iterdir(self):
     key = to_dir_key(self.key) if self.key != "" else self.key
     cur = self._client.list_objects_v2(Bucket=self.bucket,
                                        Prefix=key,
                                        Delimiter="/")
     while True:
         file_names = (c["Key"] for c in cur.get("Contents", []))
         dir_names = (c["Prefix"] for c in cur.get("CommonPrefixes", []))
         for name in itertools.chain(dir_names, file_names):
             if name in {".", "..", "", key}:
                 continue
             yield S3Path(f"{self.anchor}/{name}")
         if not cur["IsTruncated"]:
             break
         cur = self._client.list_objects_v2(
             Bucket=self.bucket,
             Prefix=key,
             Delimiter="/",
             ContinuationToken=cur["NextContinuationToken"],
         )
コード例 #8
0
 def exists(self):
     file_key = to_file_key(self.key)
     dir_key = to_dir_key(self.key)
     return self._exists(file_key) or self._exists(dir_key)
コード例 #9
0
 def is_dir(self):
     dir_key = to_dir_key(self.key)
     blob = self._client.get_bucket(self.bucket).get_blob(dir_key)
     if blob is None:
         return False
     return blob.size == 0