def list_elements(self, base_folder: str, pattern: [Optional[str]] = '*', return_absolute_paths: bool = True) \ -> List[str]: if return_absolute_paths: base_folder = os.path.abspath(base_folder) from com.obs.client.obs_client import ObsClient path_to_bucket_info_file = os.path.join(base_folder, 'bucket_info.json') file_names = [] with open(path_to_bucket_info_file, "r") as bucket_info_file: bucket_info = json.load(bucket_info_file) obs_client = ObsClient(access_key_id=self._access_key_id, secret_access_key=self._secret_access_key, server=_MUNDI_SERVER) prefixes = bucket_info['prefixes'] for prefix in prefixes: objects = obs_client.listObjects(bucketName=bucket_info['bucket'], prefix=prefix) if objects.status < 300: for content in objects.body.contents: remote_file_name = content.key.split('/')[-1] if fnmatch.fnmatch(remote_file_name, pattern): file_name = os.path.join(base_folder, remote_file_name) file_name = file_name.replace('\\', '/') file_names.append(file_name) else: logging.error(objects.errorCode) obs_client.close() return file_names
def doGetObject(lock, completedBlocks, bucketName, objectKey, startPos, endPos): global obsClient global localFilePath if obsClient is None: obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) from com.obs.models.get_object_header import GetObjectHeader resp = obsClient.getObject(bucketName, objectKey, headers=GetObjectHeader(range='%d-%d' % (startPos, endPos))) if resp.status < 300: response = resp.body.response chunk_size = 65536 if response is not None: with open(localFilePath, 'wb+') as f: f.seek(startPos) while True: chunk = response.read(chunk_size) if not chunk: break f.write(chunk) response.close() lock.acquire() try: completedBlocks.value += 1 finally: lock.release()
def __init__(self, conf=None): if not conf: self.__conf__ = get_settings_environment("pro") #缺省配置 self.__ak__ = self.__conf__.OBS_AK if conf is not None else _AK self.__sk__ = self.__conf__.OBS_SK if conf is not None else _SK self.__issecure__ = self.__conf__.OBS_ISSECURE if conf is not None else _ISSECURE self.__server__ = self.__conf__.OBS_SERVER if conf is not None else _SERVER self.__signature__ = self.__conf__.OBS_SIGNATURE if conf is not None else _SIGNATURE self.__region__ = self.__conf__.OBS_REGION if conf is not None else _REGION self.__client__ = ObsClient(access_key_id=self.__ak__, secret_access_key=self.__sk__, is_secure=self.__issecure__, server=self.__server__, signature=self.__signature__, region=self.__region__) #缺省桶名称 self.__bucketname__ = self.__conf__.OBS_BUCKETNAME #消息头 self.__header__ = PutObjectHeader(md5=self.__conf__.OBS_MD5, acl=self.__conf__.OBS_ACL, location=self.__conf__.OBS_LOCATION)
def GetObject(obsAddr, bucketName, objName, ak, sk): TestObs = ObsClient(access_key_id=ak, secret_access_key=sk, is_secure=secure, server=obsAddr, signature=signature, path_style=path_style, region=region,ssl_verify=False, port=port, max_retry_count=5, timeout=20, chunk_size=65536) LobjectRequest = GetObjectRequest(content_type='application/zip', content_language=None, expires=None, cache_control=None, content_disposition=None, content_encoding=None, versionId=None) Lheaders = GetObjectHeader(range='', if_modified_since=None, if_unmodified_since=None, if_match=None, if_none_match=None) loadStreamInMemory = False resp = TestObs.getObject(bucketName=bucketName, objectKey=objName, downloadPath=TEMP_ROOT_PATH+objName, getObjectRequest=LobjectRequest, headers=Lheaders, loadStreamInMemory=loadStreamInMemory) print('*** GetObject resp: ', resp) if isinstance(resp.body, ObjectStream): print '******' if loadStreamInMemory: print(resp.body.size) else: response = resp.body.response chunk_size = 65536 if response is not None: while True: chunk = response.read(chunk_size) if not chunk: break print(chunk) response.close() else: pass #print(resp.body)
def PostObject(obsAddr, bucket, objName, ak, sk): TestObs = ObsClient(access_key_id=ak, secret_access_key=sk, is_secure=secure, server=obsAddr, signature=signature, path_style=path_style, region=region, ssl_verify=False, port=port, max_retry_count=5, timeout=20) Lheaders = PutObjectHeader(md5=None, acl='private', location=None, contentType='text/plain') Lheaders.sseHeader = SseKmsHeader.getInstance() h = PutObjectHeader() Lmetadata = {'key': 'value'} objPath = TEMP_ROOT_PATH + objName resp = TestObs.postObject(bucketName=bucket, objectKey=objName, file_path=objPath, metadata=Lmetadata, headers=h) if isinstance(resp, list): for k, v in resp: print('PostObject, objectKey', k, 'common msg:status:', v.status, ',errorCode:', v.errorCode, ',errorMessage:', v.errorMessage) else: print('PostObject, common msg: status:', resp.status, ',errorCode:', resp.errorCode, ',errorMessage:', resp.errorMessage)
def assure_element_provided(self, name: str) -> bool: if os.path.exists(name): return True from com.obs.client.obs_client import ObsClient name = name.replace('\\', '/') base_folder = os.path.abspath(os.path.join(name, os.pardir)) path_to_bucket_info_file = f'{base_folder}/bucket_info.json' with open(path_to_bucket_info_file, "r") as bucket_info_file: bucket_info = json.load(bucket_info_file) prefixes = bucket_info['prefixes'] for prefix in prefixes: key = f"{prefix}{name.split('/')[-1]}" obs_client = ObsClient(access_key_id=self._access_key_id, secret_access_key=self._secret_access_key, server=_MUNDI_SERVER) resp = obs_client.getObject(bucketName=bucket_info['bucket'], objectKey=key, downloadPath=name) if resp.status < 300: logging.info(f"Downloaded from bucket {bucket_info['bucket']} and prefix {key} to {name}") obs_client.close() break return os.path.exists(name)
def download_data(objectURI): # Create an instance of ObsClient. obsClient = ObsClient(access_key_id='*** Provide your Access Key ***', secret_access_key='*** Provide your Secret Key ***', server='yourdomainname') # Use the instance to access OBS. resp = obsClient.getObject('bucketname', objectURI) if resp.status < 300: print('requestId:', resp.requestId) if resp.body and resp.body.response: while True: chunk = resp.body.response.read(65536) if not chunk: break print(chunk) resp.body.response.close() else: print('errorCode:', resp.errorCode) print('errorMessage:', resp.errorMessage) # Close obsClient. obsClient.close()
def doCopyPart(lock, partETags, bucketName, objectKey, partNumber, uploadId, copySource, copySourceRange): global obsClient if obsClient is None: obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) resp = obsClient.copyPart(bucketName=bucketName, objectKey=objectKey, partNumber=partNumber, uploadId=uploadId, copySource=copySource, copySourceRange=copySourceRange) if resp.status < 300: lock.acquire() try: partETags[partNumber] = resp.body.etag finally: lock.release()
def doUploadPart(lock, partETags, bucketName, objectKey, partNumber, uploadId, filePath, partSize, offset): global obsClient if obsClient is None: obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) resp = obsClient.uploadPart(bucketName, objectKey, partNumber, uploadId, filePath, isFile=True, partSize=partSize, offset=offset) if resp.status < 300: lock.acquire() try: partETags[partNumber] = resp.body.etag finally: lock.release()
def _get_from_wrapped( self, data_set_meta_info: DataSetMetaInfo) -> Sequence[FileRef]: from com.obs.client.obs_client import ObsClient if data_set_meta_info.data_type not in _DATA_TYPE_PARAMETER_DICTS: logging.warning( f'Data Type {data_set_meta_info.data_type} not supported by MUNDI DIAS File System ' f'implementation.') return [] buckets = self._get_bucket_names(data_set_meta_info) prefix = self._get_prefix(data_set_meta_info) obs_client = ObsClient(access_key_id=self._access_key_id, secret_access_key=self._secret_access_key, server=_MUNDI_SERVER) keys = [] excludes = _DATA_TYPE_PARAMETER_DICTS[ data_set_meta_info.data_type]['excludes'] right_bucket = None for bucket in buckets: right_bucket = bucket objects = obs_client.listObjects(bucketName=bucket, prefix=prefix) if objects.status < 300: for content in objects.body.contents: if data_set_meta_info.identifier in content.key: move_on = False for exclude in excludes: if content.key.endswith(exclude): move_on = True if not move_on: keys.append(content.key) if len(keys) > 0: break if len(keys) == 0: return [] data_set_id = data_set_meta_info.identifier for key in keys: relative_path_to_file = key.split(data_set_meta_info.identifier)[1] target_file = f'{self._temp_dir}/{data_set_meta_info.identifier}{relative_path_to_file}' if len(keys) == 1: data_set_id = f'{data_set_meta_info.identifier}{relative_path_to_file}' resp = obs_client.getObject(right_bucket, key, downloadPath=target_file) if resp.status >= 300: return [] obs_client.close() file_ref = FileRef(f'{self._temp_dir}/{data_set_id}', data_set_meta_info.start_time, data_set_meta_info.end_time, get_mime_type(data_set_meta_info.identifier)) return [file_ref]
import sys, ssl IS_PYTHON2 = sys.version_info.major == 2 or not sys.version > '3' if IS_PYTHON2: from urlparse import urlparse import httplib else: import http.client as httplib from urllib.parse import urlparse from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style, is_secure=False) def doAction(msg, method, url, headers=None, content=None): print(msg + ' using v2 temporary signature url:') print('\t' + url) url = urlparse(url) if headers is None: headers = {} conn = httplib.HTTPConnection(url.hostname, url.port) path = url.path + '?' + url.query
#!/usr/bin/python # -*- coding:utf-8 -*- AK = '*** Provide your Access Key ***' SK = '*** Provide your Secret Key ***' server = 'obs.myhwclouds.com' signature = 'v4' path_style = True bucketName = 'my-obs-bucket-demo' objectKey = 'my-obs-object-key-demo' from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create bucket print('Create a new bucket for demo\n') obsClient.createBucket(bucketName) content = 'Hello OBS' keyPrefix = 'MyObjectKey' from com.obs.models.delete_objects_request import DeleteObjectsRequest, Object keys = [] # First insert 100 objects for demo for i in range(100):
class OBS_Api(object): ''' desc:华为云对象存储 ''' def __init__(self, conf=None): if not conf: self.__conf__ = get_settings_environment("pro") #缺省配置 self.__ak__ = self.__conf__.OBS_AK if conf is not None else _AK self.__sk__ = self.__conf__.OBS_SK if conf is not None else _SK self.__issecure__ = self.__conf__.OBS_ISSECURE if conf is not None else _ISSECURE self.__server__ = self.__conf__.OBS_SERVER if conf is not None else _SERVER self.__signature__ = self.__conf__.OBS_SIGNATURE if conf is not None else _SIGNATURE self.__region__ = self.__conf__.OBS_REGION if conf is not None else _REGION self.__client__ = ObsClient(access_key_id=self.__ak__, secret_access_key=self.__sk__, is_secure=self.__issecure__, server=self.__server__, signature=self.__signature__, region=self.__region__) #缺省桶名称 self.__bucketname__ = self.__conf__.OBS_BUCKETNAME #消息头 self.__header__ = PutObjectHeader(md5=self.__conf__.OBS_MD5, acl=self.__conf__.OBS_ACL, location=self.__conf__.OBS_LOCATION) def set_bucket_name(self, bucketname): ''' #设置桶名称 ''' self.__bucketname__ = bucketname def set_putobjectheader(self, md5=None, acl=None, location=None): ''' #设置消息头 ''' self.__header__ = PutObjectHeader(md5=md5, acl=acl, location=location) def uploadobject(self, up_path, localpath): ''' #uploadPath:对象存储的objectkey #localPath:本地路径 ''' # metadata = kwargs.get("meta") if "meta" in kwargs else None f = open(localpath, "rb") content = f.read() f.close() resp = self.__client__.putContent(self.__bucketname__, up_path, content=content, headers=self.__header__) if resp.status > 300: print('errorCode:', resp.errorCode) print('errorMessage:', resp.errorMessage) # return self.__client__.postObject(self.__bucketname__, uploadpath, localpath,metadata=metadata,headers=self.__header__) def __getattr__(self, attr): ''' #OBS原生方法调用,按方法名称调用,例如:实例名obsapi调用对象列表方法,obsapi.listObjects(bucketName='obs-inews-01',max_keys=10) ''' def wrapper(*args, **kwargs): return getattr(self.__client__, attr)(*args, **kwargs) return wrapper
#!/usr/bin/python # -*- coding:utf-8 -*- # 引入模块 from com.obs.client.obs_client import ObsClient # 创建ObsClient实例 obsClient = ObsClient( access_key_id='UDSIAMSTUBTEST000100', secret_access_key='Udsiamstubtest000000UDSIAMSTUBTEST000100', server='obs.esdk.com', path_style=True, signature='v2', long_conn_mode=False, is_secure=False # 配置使用HTTPS协议 ) # 引入模块 from com.obs.client.obs_client import ObsClient # 创建ObsClient实例 obsClient = ObsClient(access_key_id='*** Provide your Access Key ***', secret_access_key='*** Provide your Secret Key ***', server='obs.myhwclouds.com') obsClient.abortMultipartUpload('bucketname', 'objectkey', 'upload id from initiateMultipartUpload') if resp.status < 300: print('requestId:', resp.requestId) else:
from com.obs.models.get_object_request import GetObjectRequest from com.obs.response.get_object_response import ObjectStream from com.obs.models.server_side_encryption import SseKmsHeader,SseCHeader from com.obs.log.Log import LogConf AK = '*** Provide your Access Key ***' SK = '*** Provide your Secret Key ***' server = 'obs.myhwclouds.com' region = 'CHINA' secure = True signature = 'v4' path_style = True # create ObsClient instance TestObs = ObsClient(access_key_id=AK, secret_access_key=SK, is_secure=secure, server=server, signature=signature, path_style=path_style, region=region) # init log def initLog(): TestObs.initLog(LogConf('../log.conf'), 'test_client') # create bucket def CreateBucket(): headers = CreateBucketHeader(aclControl=HeadPermission.PUBLIC_READ, storageClass='STANDARD_IA') resp = TestObs.createBucket(bucketName='bucket001', header=headers, location=None) print('common msg:status:', resp.status, ',errorCode:', resp.errorCode, ',errorMessage:', resp.errorMessage, ',resHeader:', resp.header) # delete bucket def DeleteBucket(): resp = TestObs.deleteBucket(bucketName='bucket001')
(such as do bucket ACL/CORS/Lifecycle/Logging/Website/Location/Tagging/OPTIONS) on Huawei OBS using the OBS SDK for Python. ''' AK = '*** Provide your Access Key ***' SK = '*** Provide your Secret Key ***' server = 'obs.myhwclouds.com' signature = 'v4' path_style = True bucketName = 'my-obs-bucket-demo' from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) def createBucket(): resp = obsClient.createBucket(bucketName) if resp.status < 300: print('Create bucket:' + bucketName + ' successfully!\n') else: print(resp.errorCode) def getBucketLocation(): resp = obsClient.getBucketLocation(bucketName) if resp.status < 300:
on Huawei OBS using the OBS SDK for Python. ''' AK = '*** Provide your Access Key ***' SK = '*** Provide your Secret Key ***' server = 'obs.myhwclouds.com' signature = 'v4' path_style = True bucketName = 'my-obs-bucket-demo' objectKey = 'my-obs-object-key-demo' from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create bucket print('Create a new bucket for demo\n') obsClient.createBucket(bucketName) # Create object resp = obsClient.putContent(bucketName, objectKey, 'Hello OBS') if resp.status < 300: print('Create object ' + objectKey + ' successfully!\n') # Get object metadata print('Getting object metadata') resp = obsClient.putContent(bucketName, objectKey, 'Hello OBS')
def run(): # Initialize Parameters (Proxy not Used in this script) ProxyHost = None ProxyPort = None ProxyUserName = None ProxyPassword = None Proxies = None VerifyCert = False LastMarker = None FinalMarker = None Prefix = None OBSEndpoint = "obs.eu-de.otc.t-systems.com" MaxKeys = 1000 # Read Parameters passed by Splunk Configuration # config = get_config() # Instance = config["name"] # IdpName = config["idpname"] # BucketFolder = config["bucketfolder"] # BucketName = config["bucketname"] # Prefix = config["logprefix"] # UserName = config["username"] # UserPass = config["userpass"] # CheckPoint_dir = config["checkpoint_dir"] Instance = "obs_ta_idp//VPCFLOW" IdpName = "IDP" BucketFolder = "LogTanks" BucketName = "obs-vpcflow" Prefix = "VPC" UserName = "******" UserPass = "******" CheckPoint_dir = "c:/temp" # # Setup Checkpoint file name based on Instance name. We ae parsing the name passed by Splunk slist = Instance.split("//") InstanceName = slist[1] CheckPoint = os.path.join(CheckPoint_dir, InstanceName + ".checkpoint") # Authenticate with IdP Initiated Federation and return Token (Powershell Script) TokenID = get_token(UserName, UserPass, IdpName) # Get Temporary AK/SK from IAM for Federated User AK, SK, TokenID = get_ak(TokenID, Proxies, VerifyCert) # Constructs a obs client instance with your account for accessing OBS # https://docs.otc.t-systems.com/en-us/sdk_python_api/obs/en-us_topic_0080493206.html obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, security_token=TokenID, server=OBSEndpoint, proxy_host=ProxyHost, proxy_port=ProxyPort, proxy_username=ProxyUserName, proxy_password=ProxyPassword) # We check if the last run saved the checkpoint object so that we don't process already processed logs. if os.path.exists(CheckPoint): if os.path.getsize(CheckPoint): fo = open(CheckPoint, "r+") LastMarker = fo.readline() fo.close() while True: # Start Processing Logs using the listobjects function defined above. This may cycle multiple times of more than maxkey returned. FinalMarker, FinalMarkerTag = processlogs(obsClient, BucketName, BucketFolder, CheckPoint, prefix=Prefix, marker=LastMarker, max_keys=MaxKeys, source=InstanceName) if FinalMarkerTag is None: obsClient.close() break LastMarker = FinalMarker
from Huawei OBS using the OBS SDK for Python. ''' AK = '*** Provide your Access Key ***' SK = '*** Provide your Secret Key ***' server = 'obs.myhwclouds.com' signature = 'v4' path_style = True bucketName = 'my-obs-cold-bucket-demo' objectKey = 'my-obs-cold-object-key-demo' from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create a cold bucket print('Create a new cold bucket for demo\n') from com.obs.models.create_bucket_header import CreateBucketHeader obsClient.createBucket(bucketName, CreateBucketHeader(storageClass='GLACIER')) # Create a cold object print('Create a new cold object for demo\n') obsClient.putContent(bucketName, objectKey, 'Hello OBS') # Restore the cold object print('Restore the cold object') obsClient.restoreObject(bucketName, objectKey, 1, tier='Expedited')
import http.client as httplib def createSampleFile(sampleFilePath): if not os.path.exists(sampleFilePath): dir = os.path.dirname(sampleFilePath) if not os.path.exists(dir): os.makedirs(dir, mode=0o755) import uuid with open(sampleFilePath, 'w') as f: f.write(str(uuid.uuid1()) + '\n') f.write(str(uuid.uuid4()) + '\n') return sampleFilePath from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create bucket print('Create a new bucket for demo\n') obsClient.createBucket(bucketName) # Create sample file sampleFilePath = '/temp/text.txt' createSampleFile(sampleFilePath) # Claim a post object request formParams = {'acl': 'public-read', 'content-type': 'text/plain', 'x-amz-meta-meta1': 'value1', 'x-amz-meta-meta2': 'value2'} res = obsClient.createV4PostSignature(bucketName, objectKey, expires=3600, formParams=formParams) # Start to post object
def handler(event, context): """ Function triggers each time there is a new object created in the Object Storage bucket Input: event: JSON-structure with details of the event which triggered the function context: security context of the function Output: it's optional (we will return a completion message string) """ # Get Access Key/Secret Key to work with OBS from the security context ak = context.getAccessKey() sk = context.getSecretKey() # Set up endpoint for OBS endpoint = "obs.ru-moscow-1.hc.sbercloud.ru" # Set up logger logger = context.getLogger() # Get bucket name, file name and file size from the event details bucket_name = event['Records'][0]['obs']['bucket']["name"] file_name = event['Records'][0]['obs']['object']["key"] file_size = event['Records'][0]['obs']['object']["size"] logger.info("File %s received, size is %s" % (file_name, file_size)) # We will only process files of non-zero size if (file_size > 0): # Get Auth token for Christofari X_API_KEY = os.environ['X_API_KEY'] EMAIL = os.environ['EMAIL'] PASSWORD = os.environ['PASSWORD'] resp = requests.post("https://api.aicloud.sbercloud.ru/public/v1/auth", json={ "email": EMAIL, "password": PASSWORD }, headers={"X-Api-Key": X_API_KEY}) if (resp.status_code == 200): data = resp.json() ACCESS_TOKEN = data['token']['access_token'] else: print("Couldnt authorize with these args") return "Failed to authorize with Christofari" # Open connection to OBS conn = ObsClient(access_key_id=ak, secret_access_key=sk, server=endpoint, path_style=True, region="ru-moscow-1") # Construct the local path of the incoming file local_incoming_file_name = os.path.join(os.sep, "tmp", file_name) # Download the file from the bucket resp = conn.getObject(bucket_name, file_name, local_incoming_file_name) # Open the image and convert to black & white img = Image.open(local_incoming_file_name).convert("L") # Thumbnail the image original_width, original_height = img.size new_width = 28 new_height = int(original_height * new_width / original_width) img.thumbnail((new_width, new_height), Image.ANTIALIAS) imgByteArr = BytesIO() img.save(imgByteArr, format='PNG') imgByteArr = imgByteArr.getvalue() img_b64 = base64.b64encode(imgByteArr) encoded_img_b64 = img_b64.decode("utf-8") resp = requests.post( "https://api.aicloud.sbercloud.ru/public/v1/inference/v1/predict/kfserving-1599815581/kfserving-1599815581/", headers={ "X-Api-Key": X_API_KEY, "Authorization": ACCESS_TOKEN, "Content-Type": "application/json" }, data=json.dumps( {"instances": [{ "image": { "b64": encoded_img_b64 } }]})) if (resp.status_code == 200): data = json.loads(resp.json()["body"]) prediction = data["Prediction"] print("*********") print(f"Christofari thinks that it's {prediction}") print("*********") else: print(resp.status_code) return "File processed."
This sample demonstrates how to create an empty folder under specified bucket to Huawei OBS using the OBS SDK for Python. ''' AK = '*** Provide your Access Key ***' SK = '*** Provide your Secret Key ***' server = 'obs.myhwclouds.com' signature = 'v4' path_style = True bucketName = 'my-obs-bucket-demo' from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create bucket print('Create a new bucket for demo\n') obsClient.createBucket(bucketName) keySuffixWithSlash1 = 'MyObjectKey1/' keySuffixWithSlash2 = 'MyObjectKey2/' # Create two empty folder without request body, note that the key must be suffixed with a slash obsClient.putContent(bucketName, keySuffixWithSlash1, '') print('Creating an empty folder ' + keySuffixWithSlash1) obsClient.putContent(bucketName, keySuffixWithSlash2) print('Creating an empty folder ' + keySuffixWithSlash2)
AK = 'SIQYC8WK9WFLUGFJUVI3' SK = 'vneBIoLdfccFzq5FfYyOhC5LYjgRR8ChRPXqvtwO' server = 'obs.myhwclouds.com' region = 'china' secure = False signature = 'v4' port = 80 path_style = True TestObs = ObsClient(access_key_id=AK, secret_access_key=SK, is_secure=secure, server=server, signature=signature, path_style=path_style, region=region, ssl_verify=False, port=port, max_retry_count=5, timeout=20, chuck_size=65536) def PutObject(bucketname, fileName, fileContent): ''' 引入库: from com.obs.client.obs_client import ObsClient from com.obs.models.put_object_header import PutObjectHeader from com.obs.models.server_side_encryption import SseKmsHeader,SseCHeader 函数原型:putObject(self, bucketName, objectKey, content, metadata=None, headers=PutObjectHeader()) 函数功能:上传对象 (上传内容) 参数说明: bucketName:桶名 (必填)
def handler(event, context): """ Function triggers each time there is a new object created in the Object Storage bucket Input: event: JSON-structure with details of the event which triggered the function context: security context of the function Output: it's optional (we will return a completion message string) """ # Get Access Key/Secret Key to work with OBS from the security context ak = context.getAccessKey() sk = context.getSecretKey() # Set up endpoint for OBS endpoint = "obs.ru-moscow-1.hc.sbercloud.ru" # Get bucket name from environment where # We will upload modified files there static_bucket_name = os.environ["STATIC_BUCKET_NAME"] # Set up logger logger = context.getLogger() # Get bucket name, file name and file size from the event details bucket_name = event['Records'][0]['obs']['bucket']["name"] file_name = event['Records'][0]['obs']['object']["key"] file_size = event['Records'][0]['obs']['object']["size"] logger.info("File %s received, size is %s" % (file_name, file_size)) # We will only process files of non-zero size if (file_size > 0): # Open connection to OBS conn = ObsClient(access_key_id=ak, secret_access_key=sk, server=endpoint, path_style=True, region="ru-moscow-1") # Construct the local path of the incoming file local_incoming_file_name = os.path.join(os.sep, "tmp", file_name) # Download the file from the bucket resp = conn.getObject(bucket_name, file_name, local_incoming_file_name) # Open the image im = Image.open(local_incoming_file_name) # Thumbnail the image original_width, original_height = im.size new_width = 200 new_height = int(original_height * new_width / original_width) im.thumbnail((new_width, new_height), Image.ANTIALIAS) # Convert the image to Numpy array arr = np.array(np.asarray(im)) # Change colors randomly for i in range(3): m = rd.choice([-1, 1]) arr[:, :, i] = arr[:, :, i] + m * 50 # Convert back to image result = Image.fromarray(arr) # Construct the local path of an updated image local_outgoing_file_name = os.path.join(os.sep, "tmp", "_" + file_name) # Save updated image result.save(local_outgoing_file_name) # Upload updated file to another bucket resp = conn.putFile(static_bucket_name, file_name, local_outgoing_file_name) logger.info(resp["status"]) return "File processed."
on Huawei OBS using the OBS SDK for Python. ''' AK = '*** Provide your Access Key ***' SK = '*** Provide your Secret Key ***' server = 'obs.myhwclouds.com' signature = 'v4' path_style = True bucketName = 'my-obs-bucket-demo' objectKey = 'my-obs-object-key-demo' from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create bucket print('Create a new bucket for demo\n') obsClient.createBucket(bucketName) from com.obs.models.put_object_header import PutObjectHeader # Setting object mime type headers = PutObjectHeader(contentType='text/plain') # Setting self-defined metadata metadata = {'meta1': 'value1', 'meta2': 'value2'}
f = open(file_name, 'wb') f.write(resp) f.close() return True #1. download web url to import obs #2. get obs read temp url def change_weburl_2_obs(url, client, bucket, prefix, temp_file): if not download_url_file(url, temp_file): return False, '' flag, key, url = upload_file_2_obs(client, bucket, prefix, temp_file) os.remove(temp_file) return flag, url if __name__ == "__main__": client = ObsClient(access_key_id='OBS AK', secret_access_key='OBS SK', server='obs.cn-north-1.myhwclouds.com') bucket = 'bucketXXX' print change_weburl_2_obs( 'http://c.hiphotos.baidu.com/image/pic/item/ae51f3deb48f8c540e2d1dd336292df5e1fe7f54.jpg', client, bucket, 'WEBURL', 'a.jpg') client.close() #print upload_file_2_obs(client, bucket, 'MANUAL', 'a.jpg') #print download_url_file('http://c.hiphotos.baidu.com/image/pic/item/ae51f3deb48f8c540e2d1dd336292df5e1fe7f54.jpg', 'a.jpg')
''' This sample demonstrates how to list objects under a specified folder of a bucket from Huawei OBS using the OBS SDK for Python. ''' AK = '*** Provide your Access Key ***' SK = '*** Provide your Secret Key ***' server = 'obs.myhwclouds.com' signature = 'v4' path_style = True bucketName = 'my-obs-bucket-demo' objectKey = 'my-obs-object-key-demo' from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create bucket print('Create a new bucket for demo\n') obsClient.createBucket(bucketName) content = 'Hello OBS' keyPrefix = 'MyObjectKey' folderPrefix = 'src' subFolderPrefix = 'test' from com.obs.models.delete_objects_request import DeleteObjectsRequest, Object keys = [] # First prepare folders and sub folders
using the OBS SDK for Python. ''' AK = '*** Provide your Access Key ***' SK = '*** Provide your Secret Key ***' server = 'obs.myhwclouds.com' signature = 'v4' path_style = True bucketName = 'my-obs-bucket-demo' objectKey = 'my-obs-object-key-demo' from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create bucket print('Create a new bucket for demo\n') obsClient.createBucket(bucketName) # Step 1: initiate multipart upload print('Step 1: initiate multipart upload \n') resp = obsClient.initiateMultipartUpload(bucketName, objectKey) uploadId = resp.body.uploadId # Step 2: upload a part print('Step 2: upload a part\n')
if not os.path.exists(sampleFilePath): dir = os.path.dirname(sampleFilePath) if not os.path.exists(dir): os.makedirs(dir, mode=0o755) import uuid with open(sampleFilePath, 'w') as f: f.write(str(uuid.uuid1()) + '\n') f.write(str(uuid.uuid4()) + '\n') return sampleFilePath from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create bucket print('Create a new bucket for demo\n') obsClient.createBucket(bucketName) sampleFilePath = '/temp/test.txt' # Upload an object to your bucket print('Uploading a new object to OBS from a file\n') obsClient.putFile(bucketName, objectKey, createSampleFile(sampleFilePath)) print('Downloading an object as a socket stream\n') # Download the object as a socket stream and display it directly
os.makedirs(dir, mode=0o755) import uuid index = 1000000 with open(sampleFilePath, 'w') as f: while index >= 0: f.write(str(uuid.uuid1()) + '\n') f.write(str(uuid.uuid4()) + '\n') index -= 1 return sampleFilePath from com.obs.client.obs_client import ObsClient # Constructs a obs client instance with your account for accessing OBS obsClient = ObsClient(access_key_id=AK, secret_access_key=SK, server=server, signature=signature, path_style=path_style) # Create bucket print('Create a new bucket for demo\n') obsClient.createBucket(bucketName) sampleFilePath = '/temp/test.txt' # # Upload an object to your source bucket print('Uploading a new object to OBS from a file\n') obsClient.putFile(sourceBucketName, sourceObjectKey, createSampleFile(sampleFilePath)) if os.path.exists(sampleFilePath): os.remove(sampleFilePath)