예제 #1
0
def upload(url, filename, check=True):
	creds = get_credentials()
	url = creds.get('opsmgr').get('url') + url
	multipart = MultipartEncoderMonitor.from_fields(
		fields={
			'product[file]': ('product[file]', open(filename, 'rb'), 'application/octet-stream')
		},
		callback=ProgressBar().update
	)
	response = requests.post(url,
		auth=auth(creds),
		verify=False,
		data=multipart,
		headers={ 'Content-Type': multipart.content_type }
	)
	sys.stdout.write('.100%\n')
	sys.stdout.flush()
	if response.status_code == 422:
		errors = response.json()["errors"]
		try:
			product = errors.get('product', [])
			for reason in product:
				if reason.startswith('Metadata already exists for'):
					print('-','version already uploaded')
					return response
		except:
			pass
	check_response(response, check)
	return response
예제 #2
0
def upload(url, filename, check=True):
    creds = get_credentials()
    url = creds.get('opsmgr').get('url') + url
    multipart = MultipartEncoderMonitor.from_fields(
        fields={
            'product[file]':
            ('product[file]', open(filename, 'rb'), 'application/octet-stream')
        },
        callback=ProgressBar().update)
    response = requests.post(url,
                             auth=auth(creds),
                             verify=False,
                             data=multipart,
                             headers={'Content-Type': multipart.content_type})
    sys.stdout.write('.100%\n')
    sys.stdout.flush()
    if response.status_code == 422:
        errors = response.json()["errors"]
        try:
            product = errors.get('product', [])
            for reason in product:
                if reason.startswith('Metadata already exists for'):
                    print('-', 'version already uploaded')
                    return response
        except:
            pass
    check_response(response, check)
    return response
def upload(url, input_path):
    if not os.path.exists(input_path):
        sys.exit(2)
    filesize = os.stat(input_path).st_size

    filename = os.path.basename(input_path)
    filename = urllib.parse.quote_plus(filename)

    def callback(monitor):
        print('\ruploading:     ', end="")
        print('\ruploading: ' + str(round(100*monitor.bytes_read/filesize)) + '%', end="")

    m = MultipartEncoderMonitor.from_fields(
            fields={'file': (filename, open(input_path, 'rb'))},
            callback=callback
            )

    r = requests.post(upload_url, data=m,
            headers={'Content-Type': m.content_type})

    d = r.json()
    if d['result'] == 'ng':
        sys.stderr.write('upload fail \n')
        sys.exit(1)

    print("")
    print('id=' + str(d['id']) +', mp4_filename=' + d['mp4_filename'])

    return d['id'], d['mp4_filename']
예제 #4
0
 def upload_file(self, file_object, path, callback_func):
     r = requests.get('https://cloud-api.yandex.net/v1/disk/resources/upload',
                      headers=self.api_key,
                      params={'path': path})
     upload_url = r.json().get('href')
     m = MultipartEncoderMonitor.from_fields(fields={'file': file_object}, callback=callback_func)
     r = requests.put(upload_url, data=m, headers={'Content-Type': m.content_type})
     return True if r.status_code == 201 else False
예제 #5
0
 def postFile(self, url, localPath, tempPath, query = None):
     """
     @return: False if failed, json structure if success
     """
     response = None
     httpSession = requests.Session()
     bConnectError = False
     try:
         fileName = Utils.getNameFromPath(localPath)
         tempFileHanlder = open(tempPath, 'rb')
         files = {'file': (fileName, tempFileHanlder, {'Expires': '0'})}
         if query is not None:
             query = self.prepareParams(query)
         if self.logger is not None:
             self.logger.debug('[HTTP] POST Url: ' + url)
         multipartMonitor = MultipartEncoderMonitor.from_fields(fields=files, callback=handleChunkFinish)
         multipartMonitor.filePath = localPath
         headers = self.getHeaders()
         headers['Content-Type'] = multipartMonitor.content_type
         if SyncConfiguration.EnableProxy:
             response = httpSession.post(url, params=query, data=multipartMonitor, headers=headers, timeout=600, proxies=self.getProxies(), verify=False)
         elif Utils.isMacOS():
             response = httpSession.post(url, params=query, data=multipartMonitor, headers=headers, timeout=600, proxies=self.getProxies(), verify=Utils.getCertFile())
         else:
             response = httpSession.post(url, params=query, data=multipartMonitor, headers=headers, timeout=600, proxies=self.getProxies())
         self.logger.debug('[HTTP] Response status [%s] and content: %s', str(response.status_code), response.content)
         if response.status_code == StatusCode.Success:
             responseJson = response.json()
             if responseJson.get('success') == True:
                 return responseJson
             else:
                 return False
         else:
             return self.handleFailedResponseStatus(response.url, response.status_code, response.content)
     except (ProxyConnectionError, ConnectionError) as exception:
         self.logger.debug('[HTTP] post file stopped because connection abort')
         bConnectError = True
         self.logger.error(exception, exc_info=1)
         raise exception
     except (UnAuthorizedException,
      RefreshTokenFailedException,
      HttpQueryParameterError,
      StopSyncing) as exception:
         self.logger.debug('[HTTP] stop send file to server')
         raise exception
     except Exception as exception:
         if response is not None:
             self.logger.error('[HTTP] Error response content from server: %s', response.content)
         self.logger.error(exception, exc_info=1)
         raise WebAPIException
     finally:
         SyncUiInterface.SetNetworkAvailable(not bConnectError)
         HttpStatistic.FinishedUploadByte(localPath)
         httpSession.close()
         if locals().has_key('tempFileHanlder'):
             tempFileHanlder.close()
예제 #6
0
    def postFileWithToken(self, url, localPath, tempPath, token, appName, query = None):
        """
        @return: False if failed, json structure if success
        """
        response = None
        httpSession = requests.Session()
        try:
            if Utils.isWindowsOS():
                fileName = Utils.getNameFromPath(localPath)
                tempFileHanlder = open(tempPath, 'rb')
            else:
                fileName = Utils.getNameFromPath(localPath)
                tempFileHanlder = open(tempPath, 'rb')
            files = {'file': (fileName, tempFileHanlder, {'Expires': '0'})}
            if query is not None:
                query = self.prepareParams(query)
            if self.logger is not None:
                self.logger.debug('[HTTP] POST Url: ' + url)
            multipartMonitor = MultipartEncoderMonitor.from_fields(fields=files, callback=None)
            multipartMonitor.filePath = localPath
            headersToken = {'Content-Type': multipartMonitor.content_type,
             'Auth-Token': token}
            if SyncConfiguration.EnableProxy:
                response = httpSession.post(url, params=query, data=multipartMonitor, headers=headersToken, timeout=600, verify=False)
            elif Utils.isMacOS():
                response = httpSession.post(url, params=query, data=multipartMonitor, headers=headersToken, timeout=600, verify=Utils.getCertFile())
            else:
                response = httpSession.post(url, params=query, data=multipartMonitor, headers=headersToken, timeout=600, proxies=self.getProxies())
            self.logger.debug('[HTTP] Response status [%s] and content: %s', str(response.status_code), response.content)
            if response.status_code == StatusCode.Success:
                responseJson = response.json()
                if responseJson.get('success') == True:
                    return (responseJson, None)
                else:
                    return (False, '\xe8\xaf\xb7\xe9\x87\x8d\xe8\xaf\x95')
            else:
                errorMsg = '\xe8\xaf\xb7\xe9\x87\x8d\xe8\xaf\x95'
                if 400 == response.status_code or 500 == response.status_code:
                    responseJson = response.json()
                    if -1 != responseJson.find('failed_to_process_request'):
                        errorMsg = u'\u4e91\u7aef\u6587\u4ef6\u4e0d\u5b58\u5728'
                    elif 500 == response.status_code:
                        errorMsg = u'\u60a8\u7684\u7f51\u7edc\u53ef\u80fd\u5b58\u5728\u95ee\u9898\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5'
                    else:
                        errorMsg = u'\u6743\u9650\u4e0d\u8db3\u65e0\u6cd5\u5b8c\u6210\u8be5\u64cd\u4f5c\uff0c\u8bf7\u68c0\u67e5\u60a8\u7684\u534f\u4f5c\u8005\u7b49\u7ea7'
                elif 401 == response.status_code:
                    errorMsg = u'\u7528\u6237\u5df2\u9000\u51fa' + appName
                return (False, errorMsg)
        except Exception as exception:
            self.logger.error(exception, exc_info=1)
        finally:
            httpSession.close()
            if locals().has_key('tempFileHanlder'):
                tempFileHanlder.close()

        return (False, '\xe8\xaf\xb7\xe9\x87\x8d\xe8\xaf\x95')
예제 #7
0
    def upload(self,
               path: Union[str, Path],
               progressbar: bool = False,
               enable_logging: bool = False) -> ParseResponse:
        """
        Upload a file located in `path` to http://anonfiles.com. Set
        `enable_logging` to `True` to store the URL in a global config file.

        Example
        -------

        ```
        from anonfile import AnonFile

        anon = AnonFile('my_token')
        result = anon.upload('test.txt')

        # https://anonfiles.com/9ee1jcu6u9/test_txt
        print(result.url.geturl())
        ```

        Note
        ----
        Although `anonfile` offers unlimited bandwidth, uploads cannot exceed a
        file size of 20GB in theory. Due to technical difficulties in the implementation,
        the upper cap occurs much earlier at around 500MB.
        """
        path = Path(path)
        size = os.stat(path).st_size
        options = AnonFile.__progressbar_options(None,
                                                 f"Upload: {path.name}",
                                                 unit='B',
                                                 total=size,
                                                 disable=progressbar)
        with open(path, mode='rb') as file_handler:
            fields = {
                'file': (path.name, file_handler, 'application/octet-stream')
            }
            with tqdm(**options) as tqdm_handler:
                encoder_monitor = MultipartEncoderMonitor.from_fields(
                    fields,
                    callback=lambda monitor: AnonFile.__callback(
                        monitor, tqdm_handler))
                response = self.session.post(
                    urljoin(AnonFile.API, 'upload'),
                    data=encoder_monitor,
                    params={'token': self.token},
                    headers={'Content-Type': encoder_monitor.content_type},
                    timeout=self.timeout,
                    proxies=getproxies(),
                    verify=True)
                logger.log(logging.INFO if enable_logging else logging.NOTSET,
                           "upload::%s",
                           response.json()['data']['file']['url']['full'])
                return ParseResponse(response, path, None)
예제 #8
0
    def update_input(self, file_name, callback=None):

        m = MultipartEncoderMonitor.from_fields(fields={
            'file[]': (os.path.basename(file_name), open(file_name, 'rb'))
        },
                                                callback=callback)

        r = requests.put('%s/%s' % (INPUTS, self.inputs),
                         data=m,
                         params=self.params,
                         headers={'Content-Type': m.content_type})
        r.raise_for_status()
예제 #9
0
        def run(self) -> None:
            log.info(f'Starting upload thread...')

            parent = self.parent()
            selected_filename = os.path.basename(parent.selected_file)

            # O requests por padrão não disponibiliza um jeito de rastrear o progresso de upload.
            # Por isso, utilizamos o MultipartEncoderMonitor do requests-toolbelt, que nos permite
            # setar um callback que é chamado quando há uma atualização, e por sua vez emite um sinal
            # que atualiza a ProgressBar de modo não-blocante.
            with open(parent.selected_file, 'rb') as fd:
                # Criamos o MultipartEncoderMonitor
                monitor = MultipartEncoderMonitor.from_fields(
                    fields={'file': (selected_filename, fd, 'text/plain')},
                    callback=self.upload_progress_callback)

                # Fazemos a requisição
                try:
                    r = parent.session.post(f'{server_endpoint}/upload', data=monitor,
                                            headers={'Content-Type': monitor.content_type})
                except requests.exceptions.ConnectionError:
                    log.info(f'Upload failed for {selected_filename}: connection to server failed.')
                    result_message = 'Conexão com o servidor perdida.'
                    self.signal_upload_finished.emit(result_message)
                    return
                except UploadCancelledError:
                    log.info(f'Upload for {selected_filename} cancelled.')
                    result_message = f'Upload cancelado:\n{selected_filename}'
                    self.signal_upload_finished.emit(result_message)
                    return

            # Determinamos a mensagem a ser mostrada e emitimos o sinal de upload concluído.
            if r.status_code == 200:
                log.info(f'Successfully uploaded {selected_filename}')
                result_message = f'Upload concluído:\n{selected_filename}'
            elif r.status_code == 409:
                log.info(f'Failed to upload {selected_filename}: file already exists in remote server (409).')
                result_message = f'Conflito:\nArquivo {selected_filename} já existe.'
            else:
                log.info(f'Failed to upload {selected_filename}: ({r.status_code})\n{r.json()}')
                result_message = f'({r.status_code}) Houve um erro no upload de\n{selected_filename}'
            self.signal_upload_finished.emit(result_message)
예제 #10
0
    def send_request(self, method, url, params, route_style, request_json_arg,
                     upload_file_path, proxy, timeout, observer):
        headers = {
            'Authorization': 'Bearer %s' % self._oauth2_access_token,
            'X-Runtime-Version': sys.version.split(" ")[0]
        }

        if self._user_agent is not None:
            headers.update({'User-Agent': self._user_agent})

        if self._headers:
            headers.update(self._headers)

        if timeout is None:
            timeout = self._timeout

        if proxy is None:
            proxy = self._proxy

        # The contents of the body of the HTTP request
        stream = False
        if route_style == self._ROUTE_STYLE_RPC:
            headers['Content-Type'] = 'application/json'
            if self._API_VERSION == "V1":
                headers['Accept'] = 'application/v1+json'
            elif self._API_VERSION == "V2":
                headers['Accept'] = 'application/v2+json'
            else:
                headers['Accept'] = 'application/json'
            body = request_json_arg
            if method == self._METHOD_GET:
                r = self._session.get(url,
                                      headers=headers,
                                      params=params,
                                      stream=stream,
                                      verify=True,
                                      proxies=proxy,
                                      timeout=timeout)
            elif method == self._METHOD_POST:
                r = self._session.post(url,
                                       headers=headers,
                                       params=params,
                                       json=body,
                                       stream=stream,
                                       verify=True,
                                       proxies=proxy,
                                       timeout=timeout)
            else:
                raise ValueError('Unknown method: %r' % method)
        elif route_style == self._ROUTE_STYLE_DOWNLOAD:
            r = self._session.get(url,
                                  params=params,
                                  headers=headers,
                                  stream=True,
                                  verify=True,
                                  proxies=proxy,
                                  timeout=timeout)
        elif route_style == self._ROUTE_STYLE_UPLOAD:
            file_handler = open(upload_file_path, 'rb')
            files = {
                'file': ("yifangyun-file", file_handler, {
                    'Expires': '0'
                })
            }
            if observer is not None:
                multipart_monitor = MultipartEncoderMonitor.from_fields(
                    fields=files, callback=handle_chunk)
                multipart_monitor.observer = observer
                multipart_monitor.time = time.time()
                multipart_monitor.total = os.path.getsize(upload_file_path)
            else:
                multipart_monitor = MultipartEncoderMonitor.from_fields(
                    fields=files)
            headers['Content-Type'] = multipart_monitor.content_type
            r = self._session.post(url,
                                   headers=headers,
                                   params=params,
                                   data=multipart_monitor,
                                   stream=stream,
                                   verify=True,
                                   proxies=proxy,
                                   timeout=timeout)
        else:
            raise ValueError('Unknown operation style: %r' % route_style)

        if 200 <= r.status_code <= 299:
            if route_style == self._ROUTE_STYLE_DOWNLOAD:
                raw_resp = True
            else:
                raw_resp = r.content.decode('utf-8')

            if route_style == self._ROUTE_STYLE_DOWNLOAD:
                return Response(raw_resp, r)
            else:
                return Response(raw_resp)
        else:
            response = r.json()
            request_id = response.get('request_id')
            assert request_id is not None, (
                'Expected request id in response, but cannot find in %r' %
                r.text)
            if r.status_code >= 500:
                raise InternalServerError(request_id, r.status_code, r.text)
            elif r.status_code == 400:
                raise BadInputError(request_id, r.status_code, r.text)
            elif r.status_code == 401:
                oauth2 = FangcloudOAuth2FlowBase(request_session=self._session,
                                                 request_id=request_id)
                result = oauth2.refresh_token(self._oauth2_refresh_token)
                self._oauth2_access_token = result.access_token
                self._oauth2_refresh_token = result.refresh_token
                if self._call_back is not None:
                    self._call_back.on_token_refresh(result.access_token,
                                                     result.refresh_token)
                raise TokenRefreshed(request_id)
            elif r.status_code == 429:
                err = None
                retry_after = r.headers.get('X-Rate-Limit-Reset')
                raise RateLimitError(request_id, err, int(retry_after))
            elif r.status_code in (403, 404, 409):
                raw_resp = r.content.decode('utf-8')
                return ErrorResponse(request_id, r.status_code, raw_resp)
            else:
                raise YfyAPIException(request_id, r.status_code, r.text)