Skip to content

Commit

Permalink
fix: upload custom vars not work (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 authored May 31, 2024
1 parent 26ec278 commit 3ac1cf8
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
QINIU_SECRET_KEY: ${{ secrets.QINIU_SECRET_KEY }}
QINIU_TEST_BUCKET: ${{ secrets.QINIU_TEST_BUCKET }}
QINIU_TEST_DOMAIN: ${{ secrets.QINIU_TEST_DOMAIN }}
QINIU_UPLOAD_CALLBACK_URL: ${{secrets.QINIU_UPLOAD_CALLBACK_URL}}
QINIU_TEST_ENV: "travis"
MOCK_SERVER_ADDRESS: "http://127.0.0.1:9000"
PYTHONPATH: "$PYTHONPATH:."
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Changelog
## 7.13.2(2024-05-28)
* 对象存储,修复上传回调设置自定义变量失效(v7.12.0 引入)

## 7.13.1(2024-02-21)
* 对象存储,修复上传部分配置项的兼容
* 对象存储,添加上传策略部分字段
Expand Down
2 changes: 1 addition & 1 deletion qiniu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# flake8: noqa

__version__ = '7.13.1'
__version__ = '7.13.2'

from .auth import Auth, QiniuMacAuth

Expand Down
4 changes: 2 additions & 2 deletions qiniu/services/storage/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def _form_put(
modify_time=modify_time,
mime_type=mime_type,
metadata=metadata,
params=params,
custom_vars=params,
crc32_int=crc,
up_token=up_token
)
Expand Down Expand Up @@ -194,6 +194,6 @@ def put_stream(
modify_time=modify_time,
mime_type=mime_type,
metadata=metadata,
params=params,
custom_vars=params,
up_token=up_token
)
5 changes: 5 additions & 0 deletions tests/cases/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def bucket_name():
yield os.getenv('QINIU_TEST_BUCKET')


@pytest.fixture(scope='session')
def upload_callback_url():
yield os.getenv('QINIU_UPLOAD_CALLBACK_URL')


@pytest.fixture(scope='session')
def qn_auth(access_key, secret_key):
yield Auth(access_key, secret_key)
Expand Down
143 changes: 143 additions & 0 deletions tests/cases/test_services/test_storage/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,68 @@ def test_put_data_with_metadata(
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'

@pytest.mark.parametrize('temp_file', [64 * KB], indirect=True)
def test_put_file_with_callback(
self,
qn_auth,
bucket_name,
temp_file,
commonly_options,
bucket_manager,
upload_callback_url
):
key = 'test_file_with_callback'
policy = {
'callbackUrl': upload_callback_url,
'callbackBody': '{"custom_vars":{"a":$(x:a)},"key":$(key),"hash":$(etag)}',
'callbackBodyType': 'application/json',
}
token = qn_auth.upload_token(bucket_name, key, policy=policy)
ret, info = put_file(
token,
key,
temp_file,
metadata=commonly_options.metadata,
params=commonly_options.params,
)
assert ret['key'] == key
assert ret['hash'] == etag(temp_file)
assert ret['custom_vars']['a'] == 'a'
ret, info = bucket_manager.stat(bucket_name, key)
assert 'x-qn-meta' in ret
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'

def test_put_data_with_callback(
self,
qn_auth,
bucket_name,
commonly_options,
bucket_manager,
upload_callback_url
):
key = 'put_data_with_metadata'
data = 'hello metadata!'
policy = {
'callbackUrl': upload_callback_url,
'callbackBody': '{"custom_vars":{"a":$(x:a)},"key":$(key),"hash":$(etag)}',
'callbackBodyType': 'application/json',
}
token = qn_auth.upload_token(bucket_name, key, policy=policy)
ret, info = put_data(
token,
key,
data,
metadata=commonly_options.metadata,
params=commonly_options.params
)
assert ret['key'] == key
assert ret['custom_vars']['a'] == 'a'
ret, info = bucket_manager.stat(bucket_name, key)
assert 'x-qn-meta' in ret
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'


class TestResumableUploader:
@pytest.mark.parametrize('temp_file', [64 * KB], indirect=True)
Expand Down Expand Up @@ -544,6 +606,87 @@ def test_put_stream_v2_with_metadata(
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'

@pytest.mark.parametrize('temp_file', [64 * KB], indirect=True)
def test_put_stream_with_callback(
self,
qn_auth,
bucket_name,
temp_file,
commonly_options,
bucket_manager,
upload_callback_url
):
key = 'test_put_stream_with_callback'
size = os.stat(temp_file).st_size
with open(temp_file, 'rb') as input_stream:
policy = {
'callbackUrl': upload_callback_url,
'callbackBody': '{"custom_vars":{"a":$(x:a)},"key":$(key),"hash":$(etag)}',
'callbackBodyType': 'application/json',
}
token = qn_auth.upload_token(bucket_name, key, policy=policy)
ret, info = put_stream(
token,
key,
input_stream,
os.path.basename(temp_file),
size,
None,
commonly_options.params,
commonly_options.mime_type,
part_size=None,
version=None,
bucket_name=None,
metadata=commonly_options.metadata
)
assert ret['key'] == key
assert ret['custom_vars']['a'] == 'a'
ret, info = bucket_manager.stat(bucket_name, key)
assert 'x-qn-meta' in ret
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'

@pytest.mark.parametrize('temp_file', [4 * MB + 1], indirect=True)
def test_put_stream_v2_with_callback(
self,
qn_auth,
bucket_name,
temp_file,
commonly_options,
bucket_manager,
upload_callback_url
):
part_size = 4 * MB
key = 'test_put_stream_v2_with_metadata'
size = os.stat(temp_file).st_size
with open(temp_file, 'rb') as input_stream:
policy = {
'callbackUrl': upload_callback_url,
'callbackBody': '{"custom_vars":{"a":$(x:a)},"key":$(key),"hash":$(etag)}',
'callbackBodyType': 'application/json',
}
token = qn_auth.upload_token(bucket_name, key, policy=policy)
ret, info = put_stream(
token,
key,
input_stream,
os.path.basename(temp_file),
size,
None,
commonly_options.params,
commonly_options.mime_type,
part_size=part_size,
version='v2',
bucket_name=bucket_name,
metadata=commonly_options.metadata
)
assert ret['key'] == key
assert ret['custom_vars']['a'] == 'a'
ret, info = bucket_manager.stat(bucket_name, key)
assert 'x-qn-meta' in ret
assert ret['x-qn-meta']['name'] == 'qiniu'
assert ret['x-qn-meta']['age'] == '18'

@pytest.mark.parametrize('temp_file', [30 * MB], indirect=True)
@pytest.mark.parametrize('version', ['v1', 'v2'])
def test_resume_upload(self, bucket_name, qn_auth, temp_file, version):
Expand Down

0 comments on commit 3ac1cf8

Please sign in to comment.