clouddisk
import tkinter as tk
import urllib.request
import json
import boto3
from tkinter import ttk
import datetime
from tkinter import filedialog
def getToken(aws, sNo, sName, sPass):
url = "http://69.230.251.83:8888/token"
token = {}
try:
data = "aws=" + aws + "&sNo=" + sNo + "&sName=" + urllib.parse.quote(sName) + "&sPass=" + sPass
resp = urllib.request.urlopen(url, data=data.encode())
data = resp.read().decode()
token = json.loads(data)
except Exception as err:
print(err)
return token
def login():
global client, bucket, No
if btLogin["text"] == "登录":
No = txtNo.get()
Name = txtName.get()
Pwd = txtPwd.get()
if No != "" and Name != "" and Pwd != "":
token = getToken("bucket", No, Name, Pwd)
if token:
msg["text"] = "登录成功"
btUpload.config(state=tk.NORMAL)
btDownload.config(state=tk.NORMAL)
btDelete.config(state=tk.NORMAL)
txtNo.config(state=tk.DISABLED)
txtName.config(state=tk.DISABLED)
txtPwd.config(state=tk.DISABLED)
btLogin["text"] = "退出"
client = boto3.client("s3",
aws_access_key_id=token["keyId"],
aws_secret_access_key=token["secretKey"],
aws_session_token=token["sessionToken"],
region_name="cn-northwest-1")
bucket = token["bucket"]
listFiles()
else:
msg["text"] = "登录失败"
else:
msg["text"] = "学号、姓名、密码不能空"
else:
btUpload.config(state=tk.DISABLED)
btDownload.config(state=tk.DISABLED)
btDelete.config(state=tk.DISABLED)
btLogin["text"] = "登录"
txtNo.config(state=tk.NORMAL)
txtName.config(state=tk.NORMAL)
txtPwd.config(state=tk.NORMAL)
clearTable()
def fileExists(fn):
IDS = table.get_children()
for ID in IDS:
item = table.item(ID)
s = item["values"][0]
if fn == s:
return True
return False
# 上传
def upload():
try:
fn = filedialog.askopenfilename()
if fn:
p = fn.rfind("/")
fileName = fn[p + 1:]
if not fileExists(fileName):
fobj = open(fn, "rb")
data = fobj.read()
fobj.close()
client.put_object(Bucket=bucket, Key=No + "/" + fileName, Body=data)
msg["text"] = fn + "上传成功"
values = [fileName, len(data), datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")] # 日期
table.insert("", tk.END, None, values=values)
else:
msg["text"] = fileName + "已经存在"
except Exception as err:
msg["text"] = str(err)
def download():
if table.selection():
ID = table.selection()[0]
values = table.item(ID)["values"]
# 打开另存文件对话框,,默认文件是values[0]
fn = filedialog.asksaveasfilename(initialfile=values[0])
if fn:
# 下载buckect存储桶No文件夹的values[0]文件
resp = client.get_object(Bucket=bucket, Key=No + "/" + values[0])
# resp["Body"]是二进制流
data = resp["Body"].read()
f = open(fn, "wb")
f.write(data)
f.close()
msg["text"] = values[0] + "下载成功"
else:
msg["text"] = "请选择要下载文件"
def delete():
if table.selection():
ID = table.selection()[0]
values = table.item(ID)["values"]
# 删除bucket存储桶中No文件夹的values[0]文件
client.delete_object(Bucket=bucket, Key=No + "/" + values[0])
table.delete(ID)
# 删除
def delete():
if table.selection():
ID=table.selection()[0]
values=table.item(ID)["values"]
# 删除bucket存储桶中No文件夹的values[0]文件
client.delete_object(Bucket=bucket,Key=No+"/"+values[0])
table.delete(ID)
msg["text"]=values[0]+"删除成功"
else:
msg["text"]="请选择要删除的文件"
def clearTable():
items=table.get_children()
for item in items:
table.delete(item)
def listFiles():
try:
clearTable()
resp=client.list_objects(Bucket=bucket,Prefix=No+"/")
contents=resp["Contents"]
for c in contents:
dt = (c["LastModified"]+datetime.timedelta(hours = 8)).strftime("%Y-%m-%d %H:%M:%S")
fn = c["Key"][len(No)+1:]
table.insert("",tk.END,None,values=[fn,c["Size"],dt])
except Exception as err:
msg["text"]=str(err)
client = None
bucket = ""
No = ""
root = tk.Tk()
root.title("我的云盘")
root.geometry("600x400+400+100")
frm = tk.Frame(root)
lbNo = tk.Label(frm,text="学号")
lbNo.pack(side=tk.LEFT)
txtNo = tk.Entry(frm, text="")
txtNo.pack(side=tk.LEFT)
lbName=tk.Label(frm,text="姓名")
lbName.pack(side=tk.LEFT)
txtName=tk.Entry(frm,text="")
txtName.pack(side=tk.LEFT)
lbPwd=tk.Label(frm,text="密码")
lbPwd.pack(side=tk.LEFT)
txtPwd=tk.Entry(frm,text="",show="*")
txtPwd.pack(side=tk.LEFT)
btLogin=tk.Button(frm,text="登录",command=login)
btLogin.pack(side=tk.LEFT)
frm.pack()
msg=tk.Label(root,text="message",fg="red")
msg.pack(side=tk.BOTTOM)
frm=tk.Frame(root)
btUpload=tk.Button(frm,text="上传",command=upload)
btUpload.pack(side=tk.LEFT)
btDownload=tk.Button(frm,text="下载",command=download)
btDownload.pack(side=tk.LEFT)
btDelete=tk.Button(frm,text="删除",command=delete)
btDelete.pack(side=tk.LEFT)
frm.pack()
btUpload.config(state=tk.DISABLED)
btDownload.config(state=tk.DISABLED)
btDelete.config(state=tk.DISABLED)
txtNo.insert(0,"2107134123")
txtName.insert(0,"林俊展")
txtPwd.insert(0,"St2107134123")
frm=tk.Frame(root)
# 表格
table = ttk.Treeview(frm,show="headings",selectmode="browse")
# 定义列
table["columns"] = ("文件名称", "大小", "日期")
# 设置列,列还不显示
table.column("文件名称", width=200)
table.column("大小", width=100)
table.column("日期", width=100)
table.heading("文件名称", text="文件名称")
table.heading("大小", text="大小")
table.heading("日期", text="日期")
vbar = ttk.Scrollbar(frm,orient=tk.VERTICAL,command=table.yview)
table.configure(yscrollcommand=vbar.set)
vbar.pack(side=tk.RIGHT,fill=tk.Y)
frm.pack(fill=tk.BOTH,expand=1)
table.pack(fill=tk.BOTH,expand=1)
root.mainloop()
调用API
import json
word = input("english word:")
data = {"english": word}
s = json.dumps(data)
url = "https://ightny9uii.execute-api.cn-northwest-1.amazonaws.com.cn/ceshi_01"
resp = urllib.request.urlopen(url, data=s.encode())
data = resp.read()
s = data.decode()
s = json.loads(s)
print(s)
S3存储桶上传版
import boto3
import urllib.request
import urllib.parse
import json
import os
def getToken(aws,sNo,sName,sPass):
url="http://69.230.251.83:8888/token"
token={}
try:
data="aws="+aws+"&sNo="+sNo+"&sName="+urllib.parse.quote(sName)+"&sPass="+sPass
resp=urllib.request.urlopen(url,data=data.encode())
data=resp.read().decode()
token=json.loads(data)
except Exception as err:
print(err)
return token
def uploadObject(fn,data):
try:
sNo="2107134136"
sName="唐少杰"
sPass="St2107134136"
token=getToken("bucket",sNo,sName,sPass)
if token:
#创建一个s3客户端对象client
client=boto3.client("s3",
aws_access_key_id=token["keyId"],
aws_secret_access_key=token["secretKey"],
aws_session_token=token["sessionToken"],
region_name="cn-northwest-1")
#存储桶
bucket=token["bucket"]
#上传文件(对象)
client.put_object(Bucket=bucket,Key=sNo+"/"+fn,Body=data)
print(fn,"上传成功",len(data),"bytes")
else:
print("账号错误")
except Exception as err:
print(err)
fn=input("输入文件名称:")
if os.path.exists(fn):
fobj=open(fn,"rb")
data=fobj.read()
fobj.close()
#c:\demo\abc.jpg
p=fn.rfind("\\")
fn=fn[p+1:]
uploadObject(fn,data)
S3存储桶_完整版
import tkinter as tk
import urllib.request
import json
import boto3
from tkinter import ttk
import datetime
from tkinter import filedialog
def getToken(aws, sNo, sName, sPass):
url = "http://69.230.251.83:8888/token"
token = {}
try:
data = "aws=" + aws + "&sNo=" + sNo + "&sName=" + urllib.parse.quote(sName) + "&sPass=" + sPass
resp = urllib.request.urlopen(url, data=data.encode())
data = resp.read().decode()
token = json.loads(data)
except Exception as err:
print(err)
return token
def login():
global client, bucket, No
if btLogin["text"] == "登录":
No = txtNo.get()
Name = txtName.get()
Pwd = txtPwd.get()
if No != "" and Name != "" and Pwd != "":
token = getToken("bucket", No, Name, Pwd)
if token:
msg["text"] = "登录成功"
btUpload.config(state=tk.NORMAL)
btDownload.config(state=tk.NORMAL)
btDelete.config(state=tk.NORMAL)
txtNo.config(state=tk.DISABLED)
txtName.config(state=tk.DISABLED)
txtPwd.config(state=tk.DISABLED)
btLogin["text"] = "退出"
client = boto3.client("s3",
aws_access_key_id=token["keyId"],
aws_secret_access_key=token["secretKey"],
aws_session_token=token["sessionToken"],
region_name="cn-northwest-1")
bucket = token["bucket"]
listFiles()
else:
msg["text"] = "登录失败"
else:
msg["text"] = "学号、姓名、密码不能空"
else:
btUpload.config(state=tk.DISABLED)
btDownload.config(state=tk.DISABLED)
btDelete.config(state=tk.DISABLED)
btLogin["text"] = "登录"
txtNo.config(state=tk.NORMAL)
txtName.config(state=tk.NORMAL)
txtPwd.config(state=tk.NORMAL)
clearTable()
def fileExists(fn):
IDS = table.get_children()
for ID in IDS:
item = table.item(ID)
s = item["values"][0]
if fn == s:
return True
return False
def upload():
try:
fn = filedialog.askopenfilename()
if fn:
p = fn.rfind("/")
fileName = fn[p + 1:]
if not fileExists(fileName):
fobj = open(fn, "rb")
data = fobj.read()
fobj.close()
client.put_object(Bucket=bucket, Key=No + "/" + fileName, Body=data)
msg["text"] = fn + "上传成功"
values = [fileName, len(data), datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")]
table.insert("", tk.END, None, values=values)
else:
msg["text"] = fileName + "已经存在"
except Exception as err:
msg["text"] = str(err)
def download():
if table.selection():
ID = table.selection()[0]
values = table.item(ID)["values"]
# 打开另存文件对话框,,默认文件是values[0]
fn = filedialog.asksaveasfilename(initialfile=values[0])
if fn:
# 下载buckect存储桶No文件夹的values[0]文件
resp = client.get_object(Bucket=bucket, Key=No + "/" + values[0])
# resp["Body"]是二进制流
data = resp["Body"].read()
f = open(fn, "wb")
f.write(data)
f.close()
msg["text"] = values[0] + "下载成功"
else:
msg["text"] = "请选择要下载文件"
def delete():
if table.selection():
ID = table.selection()[0]
values = table.item(ID)["values"]
# 删除bucket存储桶中No文件夹的values[0]文件
client.delete_object(Bucket=bucket, Key=No + "/" + values[0])
table.delete(ID)
def delete():
if table.selection():
ID=table.selection()[0]
values=table.item(ID)["values"]
#删除bucket存储桶中No文件夹的values[0]文件
client.delete_object(Bucket=bucket,Key=No+"/"+values[0])
table.delete(ID)
msg["text"]=values[0]+"删除成功"
else:
msg["text"]="请选择要删除的文件"
def clearTable():
items=table.get_children()
for item in items:
table.delete(item)
def listFiles():
try:
clearTable()
resp=client.list_objects(Bucket=bucket,Prefix=No+"/")
contents=resp["Contents"]
for c in contents:
dt=(c["LastModified"]+datetime.timedelta(hours = 8)).strftime("%Y-%m-%d %H:%M:%S")
fn=c["Key"][len(No)+1:]
table.insert("",tk.END,None,values=[fn,c["Size"],dt])
except Exception as err:
msg["text"]=str(err)
client=None
bucket=""
No=""
root=tk.Tk()
root.title("我的云盘")
root.geometry("600x400+400+100")
frm=tk.Frame(root)
lbNo=tk.Label(frm,text="学号")
lbNo.pack(side=tk.LEFT)
txtNo=tk.Entry(frm,text="")
txtNo.pack(side=tk.LEFT)
lbName=tk.Label(frm,text="姓名")
lbName.pack(side=tk.LEFT)
txtName=tk.Entry(frm,text="")
txtName.pack(side=tk.LEFT)
lbPwd=tk.Label(frm,text="密码")
lbPwd.pack(side=tk.LEFT)
txtPwd=tk.Entry(frm,text="",show="*")
txtPwd.pack(side=tk.LEFT)
btLogin=tk.Button(frm,text="登录",command=login)
btLogin.pack(side=tk.LEFT)
frm.pack()
msg=tk.Label(root,text="message",fg="red")
msg.pack(side=tk.BOTTOM)
frm=tk.Frame(root)
btUpload=tk.Button(frm,text="上传",command=upload)
btUpload.pack(side=tk.LEFT)
btDownload=tk.Button(frm,text="下载",command=download)
btDownload.pack(side=tk.LEFT)
btDelete=tk.Button(frm,text="删除",command=delete)
btDelete.pack(side=tk.LEFT)
frm.pack()
btUpload.config(state=tk.DISABLED)
btDownload.config(state=tk.DISABLED)
btDelete.config(state=tk.DISABLED)
txtNo.insert(0,"2107134136")
txtName.insert(0,"唐少杰")
txtPwd.insert(0,"St2107134136")
frm=tk.Frame(root)
#表格
table = ttk.Treeview(frm,show="headings",selectmode="browse")
#定义列
table["columns"] = ("文件名称", "大小", "日期")
#设置列,列还不显示
table.column("文件名称", width=200)
table.column("大小", width=100)
table.column("日期", width=100)
table.heading("文件名称", text="文件名称")
table.heading("大小", text="大小")
table.heading("日期", text="日期")
vbar = ttk.Scrollbar(frm,orient=tk.VERTICAL,command=table.yview)
table.configure(yscrollcommand=vbar.set)
vbar.pack(side=tk.RIGHT,fill=tk.Y)
frm.pack(fill=tk.BOTH,expand=1)
table.pack(fill=tk.BOTH,expand=1)
root.mainloop()