Skip to content

Commit

Permalink
Add get and set ctime
Browse files Browse the repository at this point in the history
  • Loading branch information
杨赫然 committed Oct 26, 2024
1 parent adca480 commit 84f98fa
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
15 changes: 13 additions & 2 deletions objwrapper/alioss.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@ def list_objs(self, prefix=None):
def obj_exists(self, key):
return self.bucket.object_exists(key)

def write_obj(self, data, key):
self.bucket.put_object(key, data)
def write_obj(self, data, key, ctime=-1):
headers = None
if ctime >= 0:
headers = {'x-oss-meta-ctime':str(ctime)}
self.bucket.put_object(key, data, headers=headers)

def remove_obj(self, key):
self.bucket.delete_object(key)

def stat_obj(self, key):
size = self.bucket.get_object_meta(key).headers['Content-Length']
return int(size)

def get_ctime(self, key):
headers = self.head_object(key).headers
ctime = headers.get('x-oss-meta-ctime', '')
try:
return float(ctime)
except:
return 0
22 changes: 19 additions & 3 deletions objwrapper/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ def obj_exists(self, key):

return exists

def write_obj(self, data, key):
def write_obj(self, data, key, ctime=-1):
metadata = {}
if ctime >= 0:
metadata = {"Ctime":str(ctime)}
bucket = self.bucket
if self.conf.sse_c_key:
self.client.put_object(Bucket=bucket, Key=key, Body=data, SSECustomerKey=self.conf.sse_c_key, SSECustomerAlgorithm='AES256')
self.client.put_object(Bucket=bucket, Key=key, Body=data, Metadata=metadata, SSECustomerKey=self.conf.sse_c_key, SSECustomerAlgorithm='AES256')
else:
self.client.put_object(Bucket=bucket, Key=key, Body=data)
self.client.put_object(Bucket=bucket, Key=key, Body=data, Metadata=metadata)

def remove_obj(self, key):
bucket = self.bucket
Expand All @@ -109,3 +112,16 @@ def stat_obj(self, key):
obj = self.client.get_object(Bucket=bucket, Key=key)
size = int(obj.get('ContentLength'))
return size

def get_ctime(self, key):
bucket = self.bucket
if self.conf.sse_c_key:
obj = self.client.get_object(Bucket=bucket, Key=key, SSECustomerKey=self.s3_client.conf.sse_c_key, SSECustomerAlgorithm='AES256')
else:
obj = self.client.get_object(Bucket=bucket, Key=key)
metadata = obj.get('Metadata')
ctime = metadata.get('ctime', '')
try:
return float(ctime)
except:
return 0
18 changes: 18 additions & 0 deletions tests/test_objwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def read_and_write_client(client):
assert client.stat_obj(key1) == len(data1)
assert client.stat_obj(key2) == len(data2)
assert client.stat_obj(key3) == len(data3)
assert client.get_ctime(key1) == 0
assert client.get_ctime(key2) == 0
assert client.get_ctime(key3) == 0

objs = client.list_objs()
num = 0
Expand Down Expand Up @@ -60,6 +63,21 @@ def read_and_write_client(client):
client.remove_obj(key3)
assert client.obj_exists(key3) == False

ctime = 1729934744
client.write_obj(data1, key1, ctime)
client.write_obj(data2, key2, ctime)
client.write_obj(data3, key3, ctime)
assert client.get_ctime(key1) == ctime
assert client.get_ctime(key2) == ctime
assert client.get_ctime(key3) == ctime

client.remove_obj(key1)
assert client.obj_exists(key1) == False
client.remove_obj(key2)
assert client.obj_exists(key2) == False
client.remove_obj(key3)
assert client.obj_exists(key3) == False

@pytest.mark.parametrize(
"sse_c_key",
[None, 'I6gkJwqfGiXRNx7wW7au23iznCy///Q='],
Expand Down

0 comments on commit 84f98fa

Please sign in to comment.