def put(port, url, data, au): http = HttpSession(base + ":" + port) data = http.put(url, data=json.dumps(data), headers={ 'Authorization': au, 'Content-Type': 'application/json', 'accept': 'application/json' }) return data
def put(port, url, data, au): # data = requests.put(url, data, headers={'Authorization': au, 'Content-Type': 'application/json', # 'accept': 'application/msgpack'}) http = HttpSession(base + ":" + port) data = http.put(url, data=json.dumps(data), headers={ 'Authorization': au, 'Content-Type': 'application/json', 'accept': 'application/msgpack' }) if data.status_code == 204: return data elif data.content == b'': #返回值为空 return data else: return Http_esponseDecode(data)
class DocStorageUser(HttpUser): wait_time = constant(1) host = f"https://{BUCKET_NAME}.s3.amazonaws.com" link_signer = None file_id = None logger = None def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) success = EventHook() success.add_listener(self.success) failed = EventHook() failed.add_listener(self.failed) self.client = HttpSession(base_url=self.host, request_success=success, request_failure=failed) self.client.keep_alive = True if self.environment.aws_profile: aws_session = AwsSession.create( profile=self.environment.aws_profile) else: aws_session = AwsSession.create() self.link_signer = S3LinkSigner.create(aws_session=aws_session) def success(self, **kwargs): self.logger = self.logger.bind(**kwargs) self.logger.info(f"Successful S3 transfer") def failed(self, **kwargs): output = { "url": kwargs['exception'].request.url, "verb": kwargs['request_type'], "headers": {k: v for k, v in kwargs['exception'].request.headers.items()}, "response_time": kwargs['response_time'], "error": str(kwargs['exception']), } self.logger = self.logger.bind(**output) self.logger.error(f"Failed to process S3 transfer") @task() def start_upload(self): #self.file_id = uuid4() self.logger = get_logger(log_context={"file_id": self.file_id}) #shutil.copy(self.environment.source_file, f"{self.environment.working_directory}/{self.file_id}") object_name = f"testdocs/{self.file_id}" content_type = "application/octet-stream" start = time() link = self.link_signer.generate_presigned_put( bucket_name=BUCKET_NAME, object_name=object_name, content_type=content_type, timeout_in_seconds=LINK_TIMEOUT_VALUE) with open( f"{self.environment.working_directory}/{self.environment.source_file}", "rb") as f: end = time() self.logger.info( f"Uploading file, link age is {end - start} seconds") upload_response = self.client.put( link, data=f, headers={"Content-Type": content_type}, timeout=3000) pass