相关疑难解决方法(0)

python中的AWS Lambda导入模块错误

我正在创建一个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)

python amazon-web-services aws-lambda

70
推荐指数
11
解决办法
10万
查看次数

在python中设置压缩文件的权限

我有一个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)

python zip zipfile

7
推荐指数
2
解决办法
4338
查看次数

在Django中为生成的zip文件设置正确的权限

我正在使用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文件(在此示例中包含单个文件),我可以提取内容.但是,唯一的问题是,生成的文件的权限既不readwrite我的默认用户,也不是我的网站用户.

如何在下载之前更改自动生成文件的权限?

更新:

我已尝试使用,os.chmod并且os.fchmod正如迈克所建议的那样,但这需要一个路径名(我没有)或给出错误(for fchmod):

ZipFile instance has no attribute '__trunc__'

我想,一个选项是首先保存zip文件,设置权限,然后允许下载,但这似乎有点矫枉过正 - 必须有更好的方法来克服这个简单的问题.任何人有任何建议或想法?

Update2:

似乎这个问题仅限于Unix系统,因为它在Windows和(显然)OS X中运行良好.我在这里找到了类似的线程.据我所知,它必须与writestr方法有关.如何设置添加到zip文件的文件的权限writestr

python django permissions zip

7
推荐指数
1
解决办法
1795
查看次数

保留ZipFile中的文件属性

我正在寻找一种方法来保存写入zipfile.ZipFile实例的文件的文件属性(例如只读).

我添加到zip存档的文件会重置其文件属性,例如.在使用zip应用程序检查存档时以及解压缩后,只读标志已消失.

我当前的环境是Windows,我遇到了ZipInfo.external_attr方法的问题.

当然,写入时必须有一种标准的方法来保存文件属性ZipFile

python zip file zipfile

5
推荐指数
1
解决办法
3708
查看次数

如何将符号链接文件从Linux复制到Windows,然后再复制回Linux,但仍将其保留为符号链接

我的Linux机器中有一个符号链接。

我只想将符号链接(而非目标)复制到Windows机器,然后将该符号链接从Windows机器复制回其他Linux机器,并且该符号链接应继续工作。

我试过的

  1. 我压缩了符号链接
  2. 使用WinSCP将压缩文件移至Windows计算机
  3. 提取的符号链接
  4. 使用WinSCP将符号文件移动到Linux计算机

现在,此文件不再是符号链接。

有人知道这样做的窍门吗?

linux windows symlink gzip winscp

5
推荐指数
1
解决办法
1万
查看次数