Public URLs For Objects In Bluemix Object Storage Service

后端 未结 3 1300
陌清茗
陌清茗 2020-12-06 08:08

I would like to upload a number of photos to the Bluemix Object Storage service and then display them in a web app. Right now a GET request to the photo in the object stora

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 08:45

    Now BlueMix has an S3 endpoint capability. You can use curl or any other langage for exemple here is an boto3 that will upload an object, make it public and and some metadata : ( the function is using a json file on which you store the credentials and it uses 3 variables that are used in the global app : currentdirpath,ImagesToS3,ImageName )

    def UploadImageDansBucket (currentdirpath,ImagesToS3,ImageName) :
        currentdirpath = 'path/to/your/dir/current'
        ImagesToS3 = ' /path/of/your/object/'
        ImageName = 'Objectname'
        with open("credentials.json", 'r') as f:
            data = json.loads(f.read())
            bucket_target = data["aws"]["targetBucket"]
            print ('Open Connection to the bucket in the cloud..........')  
    
            s3ressource = boto3.resource(
                service_name='s3', 
                endpoint_url= data["aws"]["hostEndPoint"],
                aws_access_key_id= data["aws"]["idKey"],
                aws_secret_access_key=data["aws"]["secretKey"],
                use_ssl=True,
                )
            s3ressource.meta.client.meta.events.unregister('before-sign.s3', fix_s3_host)
            s3ressource.Object(bucket_target, 'hello.txt').put(Body=b"I'm a test file")
            s3ressource.Object(bucket_target, 'bin.txt').put(Body=b"0123456789abcdef"*10000)
            fn = "%s%s" % (ImagesToS3,ImageName)
            data = open(fn, 'rb')
            #s3ressource.Bucket(bucket_target).put_object(Key=fn, Body=data)
            now = datetime.datetime.now()  # on recupere la date actuelle 
            timestamp = time.mktime(now.timetuple())  # on effectue la convertion
            timestampstr = str (timestamp)
            s3ressource.Bucket(bucket_target).upload_file(fn,ImageName, ExtraArgs={ "ACL": "public-read", "Metadata": {"METADATA1": "a" ,"METADATA2": "b","METADATA3": "c", "timestamp": timestampstr },},)
    

提交回复
热议问题