我正在创建一个AWS Lambda python部署包.我正在使用一个外部依赖请求.我使用AWS文档http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html安装了外部依赖项.下面是我的python代码.
import requests
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
s3.download_file(bucket,key, '/tmp/data.txt')
lines = [line.rstrip('\n') for line in open('/tmp/data.txt')]
for line in lines:
col=line.split(',')
print(col[5],col[6])
print("CONTENT TYPE: " + response['ContentType'])
return response['ContentType']
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure …Run Code Online (Sandbox Code Playgroud) 我有一个test.txtzip存档内的文件test.zip.test.txt当压缩时,权限不受我的控制,但现在我希望它们可以进行组写.我正在使用Python解压缩文件,并且不想逃避到shell.
编辑: 这是我到目前为止所得到的:
import zipfile
z = zipfile.ZipFile('test.zip', 'w')
zi = zipfile.ZipInfo('test.txt')
zi.external_attr = 0777 << 16L
z.writestr(zi, 'FOO')
z.close()
z = zipfile.ZipFile('test.zip', 'r')
for name in z.namelist():
newFile = open(name, "wb")
newFile.write(z.read(name))
newFile.close()
z.close()
Run Code Online (Sandbox Code Playgroud)
这在使用2.5.1的OS X上完美运行,但它在我的主页框(Debian,Python 2.4和2.5)或使用Python 2.4的RHEL 5上不起作用.除了OS X之外,它不会出错,但也不会更改权限.有什么想法吗?另外,writestr()工作怎么样?我知道我在这里错误地使用它.
有没有办法在没有的情况下执行此操作os.chmod(提取文件的用户在提取os.chmod后没有权限使用)?我对zip文件有完全写入权限.
更多信息:
> ls -l test.zip
-rwxrwxrwx 1 myuser mygroup 2008-11-11 13:24 test.zip
> unzip test.zip
Archive: test.zip
inflating: test.txt
> ls -l test.txt
-rw-r--r-- 1 myuser …Run Code Online (Sandbox Code Playgroud) 我正在使用Django和Python2.6为每个用户生成自定义渲染的Django模板的zip文件,以下载定制zip文件.目前,代码views.py如下所示:
def download(request):
response = HttpResponse(mimetype='application/x-zip-compressed')
response['Content-Disposition'] = 'attachment; filename=download.zip'
myzip = zipfile.ZipFile(response, 'w')
now = datetime.datetime.now()
zipInfo = zipfile.ZipInfo('thefile.txt', (now.year, now.month, now.day, now.hour, now.minute, now.second))
myzip.writestr(zipInfo, render_to_string('template.txt', locals(), context_instance=RequestContext(request)))
myzip.close()
return response
Run Code Online (Sandbox Code Playgroud)
大多数情况下,这工作正常:txt正确下载zip文件(在此示例中包含单个文件),我可以提取内容.但是,唯一的问题是,生成的文件的权限既不read是write我的默认用户,也不是我的网站用户.
如何在下载之前更改自动生成文件的权限?
更新:
我已尝试使用,os.chmod并且os.fchmod正如迈克所建议的那样,但这需要一个路径名(我没有)或给出错误(for fchmod):
ZipFile instance has no attribute '__trunc__'
我想,一个选项是首先保存zip文件,设置权限,然后允许下载,但这似乎有点矫枉过正 - 必须有更好的方法来克服这个简单的问题.任何人有任何建议或想法?
Update2:
似乎这个问题仅限于Unix系统,因为它在Windows和(显然)OS X中运行良好.我在这里找到了类似的线程.据我所知,它必须与writestr方法有关.如何设置添加到zip文件的文件的权限writestr?
我正在寻找一种方法来保存写入zipfile.ZipFile实例的文件的文件属性(例如只读).
我添加到zip存档的文件会重置其文件属性,例如.在使用zip应用程序检查存档时以及解压缩后,只读标志已消失.
我当前的环境是Windows,我遇到了ZipInfo.external_attr方法的问题.
当然,写入时必须有一种标准的方法来保存文件属性ZipFile?
我的Linux机器中有一个符号链接。
我只想将符号链接(而非目标)复制到Windows机器,然后将该符号链接从Windows机器复制回其他Linux机器,并且该符号链接应继续工作。
我试过的
现在,此文件不再是符号链接。
有人知道这样做的窍门吗?