Integrate Xiaomi FDS
When you create or delete an object on Xiaomi FDS, Cloud Functions can automatically detect the occurrence of the event based on the FDSTrigger you created. This also triggers the execution of other related function(s).
Cloud Functions currently supports both the new and the old FDS. It can be used in both Python2 and Python3 runtime environments.
If you want to create a thumbnail for image uploaded to the bucket specified in FDS, please refer to the following code:
from fds import GalaxyFDSClient,GalaxyFDSClientException,FDSClientConfiguration
import os,sys
from PIL import Image
before_file = DOWNLOAD_IMAGE_FROM_FDS
after_file = UPLOAD_THUMBNAIL_TO_FDS
class FDSConfig:
def __init__(self, AK, SK, ENDPOINT):
self.AK = AK
self.SK = SK
self.ENDPOINT = ENDPOINT
def get_client():
fds_config = _get_FDSConfig()
config = FDSClientConfiguration()
if fds_config.ENDPOINT == None:
print "endpoint is None, please check env $XIAOMI_FDS_ENDPOINT"
return
config.set_endpoint(fds_config.ENDPOINT)
return GalaxyFDSClient(fds_config.AK, fds_config.SK,config)
def _get_FDSConfig():
AK = os.environ.get("AK")
SK = os.environ.get("SK")
ENDPOINT = os.environ.get("XIAOMI_FDS_ENDPOINT")
return FDSConfig(AK, SK, ENDPOINT)
def get_image_from_fds(client,bucket):
file = open(before_file, "w+")
try:
image_object = client.get_object(bucket,object)
for chunk in image_object.stream:
file.write(chunk)
except GalaxyFDSClientException as e:
print e.message
finally:
file.close()
def save_thumbnail():
im = Image.open(before_file)
size = (70,15)
im.thumbnail(size)
im.save(after_file,"png")
im.close()
def put_image_to_fds(client,bucket):
object_name = after_file.split("/")[-1]
image = open(after_file)
client.put_object(bucket,object_name,image)
return object_name
def main(event):
bucket = event['bucketName']
object = event['objectName']
print "start function"
client = get_client()
if not client.does_bucket_exist(bucket) :
print "bucket [%s] not exist ,please create first" %bucket
return False
get_image_from_fds(client,bucket)
save_thumbnail()
after_object_name = put_image_to_fds(client,bucket)
if client.does_object_exists(bucket, after_object_name):
print "thunmbnail has uploaded to FDS , object is [%s]" %after_object_name
return True
return False
When you use this function, please refer to the following figure to set environment variables AK, SK and XIAOMI_FDS_ENDPOINT. Sample code uses these three environment variables to call FDS sdk, Complete acquisition and upload the picture. Please also modify in the code these two variables: "before_file "," "after_file".
Please pay attention to AK/SK parameters. If you use an older version of FDS, you need to get AK/SK from dev.mi.com's Key Center. If you use the latest version of FDS, please obtain the AK/SK event parameter from User Management as dict type. The content is as follows. Please encode according to your requirements. Action field has two values: "PutObject". "DeleteObject":
{
"bucketName":"test-fds",
"objectName":"image.png",
"action":"ACTION",
"uuid":"3535a4ce-010b-42f2-bce9-ffb46d421aa5",
"timestamp":1506305123069,
"versionId":"12345"
}