Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
rsonghuster committed Jun 1, 2022
0 parents commit 93b4db6
Show file tree
Hide file tree
Showing 11 changed files with 2,651 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/registry-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: publish package to serverless-hub

on:
release:
types: [created]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- uses: actions/setup-node@v1
with:
node-version: 12
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
pip install requests
- name: Add publish file
run: |
wget https://serverless-registry.oss-cn-hangzhou.aliyuncs.com/publish-file/python3/hub-publish.py
ls
- name: Publish package
env:
publish_token: ${{ secrets.alibaba_registry_publish_token }}
run: |
ls
python hub-publish.py
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.s
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Serverless Devs Registry

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
102 changes: 102 additions & 0 deletions publish.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
Type: Application
Name: start-unzip-oss
Version: 0.0.1
Provider:
- 阿里云
Description: 使用函数计算自动解压上传到OSS指定前缀目录的zip文件
HomePage: https://github.com/devsapp/start-unzip-oss
Tags:
- OSS上zip文件自动解压
- 函数计算 OSS 触发器
Category: 其它
Service:
函数计算:
Authorities:
- AliyunFCFullAccess
对象存储:
Authorities:
- AliyunOSSFullAccess
Parameters:
type: object
additionalProperties: false # 不允许增加其他属性
required: # 必填项
- region
- serviceName
- functionName
- roleArn
- BucketName
- Prefix
- ProcessedDir
- RetainFileName
properties:
region:
title: 地域
type: string
default: cn-hangzhou
description: 创建应用所在的地区
enum:
- cn-beijing
- cn-hangzhou
- cn-shanghai
- cn-qingdao
- cn-zhangjiakou
- cn-huhehaote
- cn-shenzhen
- cn-chengdu
- cn-hongkong
- ap-southeast-1
- ap-southeast-2
- ap-southeast-3
- ap-southeast-5
- ap-northeast-1
- eu-central-1
- eu-west-1
- us-west-1
- us-east-1
- ap-south-1
serviceName:
title: 服务名
type: string
default: unzip-oss
description: 应用所属的函数计算服务
functionName:
title: 函数名
type: string
default: oss-invoke-fc
description: 应用的函数名称
roleArn:
title: RAM角色ARN
type: string
default: ''
pattern: '^acs:ram::[0-9]*:role/.*$'
description: 应用所属的函数计算服务配置的 role, 请提前创建好对应的 role, 授信函数计算服务, 并配置好 AliyunOSSFullAccess 和 AliyunFCDefaultRolePolicy policy。
required: true
x-role:
name: fcunzipossrole
service: fc
authorities:
- AliyunOSSFullAccess
- AliyunFCDefaultRolePolicy
BucketName:
title: OSS存储桶名
type: string
default: dst
description: OSS存储桶名(注意和函数同地域)
Prefix:
title: 前缀
type: string
default: src
description: 建议设置精准的前缀,同一个 Bucket 下的不同触发器条件不能重叠包含
ProcessedDir:
title: 解压目标目录
type: string
default: dst
description: 将匹配到的文件解压到此目标目录下。为防止循环触发产生不必要的费用,建议您设置不同于前缀的目标目录。
RetainFileName:
title: 是否保留压缩文件名为路径目录
type: string
default: false
description: 是否保留压缩文件名为路径目录? 若为 true, 您的目标目录为target, 压缩文件名为source.zip, 则解压后文件路径为target/source/..., 否则为 target/...。
enum:
- true
- false
82 changes: 82 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# start-unzip-oss 帮助文档

<p align="center" class="flex justify-center">
<a href="https://www.serverless-devs.com" class="ml-1">
<img src="http://editor.devsapp.cn/icon?package=start-unzip-oss&type=packageType">
</a>
<a href="http://www.devsapp.cn/details.html?name=start-unzip-oss" class="ml-1">
<img src="http://editor.devsapp.cn/icon?package=start-unzip-oss&type=packageVersion">
</a>
<a href="http://www.devsapp.cn/details.html?name=start-unzip-oss" class="ml-1">
<img src="http://editor.devsapp.cn/icon?package=start-unzip-oss&type=packageDownload">
</a>
</p>

<description>

> ***快速部署自动解压上传到OSS指定前缀目录的zip文件的应用到阿里云函数计算***
</description>

<table>
</table>

<codepre id="codepre">

</codepre>

<deploy>

## 部署 & 体验

<appcenter>

- :fire: 通过 [Serverless 应用中心](https://fcnext.console.aliyun.com/applications/create?template=start-unzip-oss)
[![Deploy with Severless Devs](https://img.alicdn.com/imgextra/i1/O1CN01w5RFbX1v45s8TIXPz_!!6000000006118-55-tps-95-28.svg)](https://fcnext.console.aliyun.com/applications/create?template=start-unzip-oss) 该应用。

</appcenter>

- 通过 [Serverless Devs Cli](https://www.serverless-devs.com/serverless-devs/install) 进行部署:
- [安装 Serverless Devs Cli 开发者工具](https://www.serverless-devs.com/serverless-devs/install) ,并进行[授权信息配置](https://www.serverless-devs.com/fc/config)
- 初始化项目:`s init start-unzip-oss -d start-unzip-oss`
- 进入项目,并进行项目部署:`cd start-unzip-oss && s deploy -y`

</deploy>

<appdetail id="flushContent">

# 应用详情

![](https://img.alicdn.com/imgextra/i1/O1CN01V092Oa1p04ieFtASz_!!6000000005297-2-tps-1220-320.png)

通过给函数创建 OSS 触发器, 用户只需要给对应的 OSS 指定前缀目录上传 zip 文件, 就会自动触发解压函数执行, 解压函数会把解压后的文件和文件夹转存回 OSS

通过 Serverless Devs 开发者工具,您只需要几步,就可以体验 Serverless 架构,带来的降本提效的技术红利。

## Tips:
上面的示例, 解压保存回去的都还是本身触发函数的 OSS, 如果是保存到其他 OSS, 直接将 `bucket.put_object(newKey + name, file_obj)` 这样上传回 oss 的代码修改下即可, 比如:

```python
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
dst_bucket_name = "xxx-bucket"
dst_bucket = oss2.Bucket(auth, endpoint, dst_bucket_name)
dst_bucket.put_object(newKey + name, file_obj)
```

</appdetail>

<devgroup>

## 开发者社区

您如果有关于错误的反馈或者未来的期待,您可以在 [Serverless Devs repo Issues](https://github.com/serverless-devs/serverless-devs/issues) 中进行反馈和交流。如果您想要加入我们的讨论组或者了解 FC 组件的最新动态,您可以通过以下渠道进行:

<p align="center">

| <img src="https://serverless-article-picture.oss-cn-hangzhou.aliyuncs.com/1635407298906_20211028074819117230.png" width="130px" > | <img src="https://serverless-article-picture.oss-cn-hangzhou.aliyuncs.com/1635407044136_20211028074404326599.png" width="130px" > | <img src="https://serverless-article-picture.oss-cn-hangzhou.aliyuncs.com/1635407252200_20211028074732517533.png" width="130px" > |
|--- | --- | --- |
| <center>微信公众号:\`serverless\`</center> | <center>微信小助手:\`xiaojiangwh\`</center> | <center>钉钉交流群:\`33947367\`</center> |

</p>

</devgroup>
52 changes: 52 additions & 0 deletions src/code/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
import oss2
from oss2 import utils, models
import ossZipfile as zipfile

zipfile_support_oss = zipfile

# support upload to oss as a file-like object


def make_crc_adapter(data, init_crc=0):
data = utils.to_bytes(data)
# file-like object
if hasattr(data, 'read'):
return utils._FileLikeAdapter(data, crc_callback=utils.Crc64(init_crc))


utils.make_crc_adapter = make_crc_adapter


class OssStreamFileLikeObject(object):
def __init__(self, bucket, key):
super(OssStreamFileLikeObject, self).__init__()
self._bucket = bucket
self._key = key
self._meta_data = self._bucket.get_object_meta(self._key)

@property
def bucket(self):
return self._bucket

@property
def key(self):
return self._key

@property
def filesize(self):
return self._meta_data.content_length

def get_reader(self, begin, end):
begin = begin if begin >= 0 else 0
end = end if end > 0 else self.filesize - 1
end = end if end < self.filesize else self.filesize - 1
begin = begin if begin < end else end
return self._bucket.get_object(self._key, byte_range=(begin, end))

def get_content_bytes(self, begin, end):
reader = self.get_reader(begin, end)
return reader.read()

def get_last_content_bytes(self, offset):
return self.get_content_bytes(self.filesize-offset, self.filesize-1)
Loading

0 comments on commit 93b4db6

Please sign in to comment.