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 105
| import oss2 import json import datetime import pytz
tz = pytz.timezone('Asia/Shanghai') auth = oss2.Auth('<你的阿里云AccessKey>', '<你的阿里云AccessKeySecret>') endpoint = 'oss-cn-hongkong-internal.aliyuncs.com'
bucketName = '<你的bucketName>' bucket = oss2.Bucket(auth, endpoint, bucketName)
message = ""
def objsize(obj): dict = {0: 'B', 1: 'KB', 2: 'MB', 3: 'GB'} size = obj.size cnt = 0 while (size / 1024 >= 1 and cnt < 3): size /= 1024 cnt += 1 return str(round(size, 2)) + dict[cnt]
def objtime(obj): timestamp = obj.last_modified time = datetime.datetime.fromtimestamp(timestamp, tz) return time.strftime("%Y-%m-%d %H:%M:%S")
def writeitem(obj, folder): if (len(folder) != len(obj.key) and obj.key.find('index.html') == -1): if (obj.is_prefix()): return "<tr><td><a href='/" + obj.key + "'>" + obj.key[len(folder):] + "</a></td><td></td><td></td></tr>" else: return "<tr><td><a href='/" + obj.key + "'>" + obj.key[len(folder):] + "</a></td><td>" + objsize( obj) + "</td><td>" + objtime(obj) + "</td></tr>\n" else: return ""
def dfs(folder, flag): global message html = "<!DOCTYPE html><html><head><meta http-equiv=\"content-type\" content=\"txt/html; charset=utf-8\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css\"><title>Index of /" + folder + "</title></head><body><div class=\"container\"><div class=\"row\"><h1>Index of /" + folder + "</h1><hr><table class=\"table table-striped table-hover\"><thead><tr><th>File name</th><th>File Size</th><th>Date</th></tr></thead><tbody>" if (folder != ''): html += "<tr><td><a href='../'>../</a></td><td></td><td></td></tr>\n" for obj in oss2.ObjectIterator(bucket, prefix=folder, delimiter='/'): if obj.is_prefix(): html += writeitem(obj, folder) for obj in oss2.ObjectIterator(bucket, prefix=folder, delimiter='/'): if not obj.is_prefix(): html += writeitem(obj, folder) html += "</table><hr></div></div></body></html>" url = folder + "index.html" result = bucket.put_object(url, html) message += 'Url:{0}'.format(url) + '\n' message += 'HTTP status: {0}'.format(result.status) + '\n' if (flag): for obj in oss2.ObjectIterator(bucket, prefix=folder, delimiter='/'): if obj.is_prefix(): dfs(obj.key, flag)
def handler(event, context): result = bucket.head_object('event.txt') bucket.append_object('event.txt',result.content_length,event) global message message = "" eventObj = json.loads(event)["events"] url = eventObj[0]['oss']['object']['key'] eventName = eventObj[0]['eventName'] if (url.find('index.html') == -1): if (len(url) - 1 == url.rfind('/')): dfs(url,False) url = url[:-1] if (url.find('/') == -1): dfs(folder='', flag=False) else: dfs(url[:url.rfind('/') + 1], False) return message else: return 'Not Modified'
|