# -*- coding: utf-8 -*-
import sae.storage
import web
import zipfile
from datetime import datetime
class Upload:
def POST(self):
web.header("Content-Type","text/html; charset=utf-8")
x=web.input(myfile={})
if 'myfile' in x and x.myfile.filename!='':
self.file_upload(x)
web.seeother('/')
def file_upload(self,x):
filepath=x.myfile.filename.replace('\\','/')
filename=filepath.split('/')[-1]
filename=datetime.now().strftime("%Y%m%d%H%M%S%f")+'.'+filename.split('.')[-1]#将文件名修改为当前日期,方便后面排序
if ".zip" in filename:
self.unzip_upload(x.myfile.file)
else:
st=sae.storage.Client()
ob=sae.storage.Object(x.myfile.file.read())
sturl=st.put('yesyouknow2',filename,ob)
def unzip_upload(self,zip_file):
st=sae.storage.Client()
z=zipfile.ZipFile(zip_file)
namelist=z.namelist()
for name in namelist:
file=z.read(name)
filename=datetime.now().strftime("%Y%m%d%H%M%S%f")+'.'+name.split('.')[-1]#日期加上文件后缀名
ob=sae.storage.Object(file)
st.put('yesyouknow2',filename,ob)
3.接受chrome扩展发送过来的图片链接:backupdate.py
#coding:utf-8
##接收chrome插件上传的图片链接,将图片下载存储到storage中.
import sae
import web
import urllib2
import sae.storage
from datetime import datetime
class Backupdate:
def GET(self):
web.header('Access-Control-Allow-Origin','*')
user_data=web.input(src="no_exist")
img_src=user_data.src
img_data=urllib2.urlopen(img_src).read()
filename=datetime.now().strftime("%Y%m%d%H%M%S%f")+'.'+img_src.split('.')[-1]#将文件名修改为当前日期,方便后面排序
st=sae.storage.Client()
ob=sae.storage.Object(img_data)
st.put('yesyouknow2',filename,ob)
return "ok",img_src
4.至于chrome扩展的话就更简单了,只用了一个函数用来发送当前的图片链接给yesyouknow网站.