Ali oss

The installation

  • Add dependencies to your Maven project
    implementation 'com. Aliyun. The dpa: oss - android SDK: 2.9.5'
Copy the code
  • Required Android permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
                    
Copy the code
  • Confuse configuration
-keep class com.alibaba.sdk.android.oss. * *{*; } -dontwarn okio.** -dontwarn org.apache.commons.codec.binary.**Copy the code

Initialize the

Initialize an instance of OSSClient whose life cycle is consistent with that of the application. Create an ossClient when the application starts and destroy it when the application ends.

  • Set the EndPoint and credentials

Using STS authentication mode, it is recommended to use OSSAuthCredentialProvider way direct access to the application server, token can be automatically updated when expired.

The following is an example of setting an EndPoint and CredentialProvider:

String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";

String stsServer = "STS application server address, such as http://abc.com"
/ / it is recommended to use OSSAuthCredentialsProvider. The token can be updated as soon as it expires.
OSSCredentialProvider credentialProvider = new OSSAuthCredentialsProvider(stsServer);

OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider);
			
Copy the code
  • Enable logging

By calling osslog.enablelog (), logs can be seen on the console, and a log file can be written to the SD card of the mobile phone. OSSLog/logs.csv is not enabled by default. Logs record data requested by oss operations and return data. Exception information, such as requestId and Response Header

OSSLog.enableLog();  // Call this method to enable logging
Copy the code
  • Setting Network Parameters
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";

// It is recommended to use STS to initialize OSSClient on mobile.
OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider("<StsToken.AccessKeyId>"."<StsToken.SecretKeyId>"."<StsToken.SecurityToken>");


ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // The connection timed out, 15 seconds by default.
conf.setSocketTimeout(15 * 1000); // The socket timed out, 15 seconds by default.
conf.setMaxConcurrentRequest(5); // Maximum number of concurrent requests, default 5.
conf.setMaxErrorRetry(2); // Maximum number of retries after a failure. The default value is 2.

OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf);
			
Copy the code
  • Some description of synchronous interface and asynchronous interface in SDK

A synchronous interface calls and blocks waiting for the result to return, whereas an asynchronous interface needs to pass in a callback function on request, where the result of the execution of the request is processed.

The synchronization interface cannot be called from the UI thread. When an exception is encountered, ClientException or ServiceException is thrown directly. The former refers to the exceptions encountered locally, such as network exceptions and invalid parameters. The latter refers to service exceptions returned by OSS, such as authentication failures and server errors.

When an asynchronous request encounters an exception, the exception is handled in the callback function.

When an asynchronous interface is called, the function returns a Task directly, which can be canceled, wait until complete, or simply fetch the result. Such as:

OSSAsyncTask task = oss.asyncGetObejct(...) ; task.cancel();// You can cancel the task
task.waitUntilFinished(); // Wait until the task is complete
GetObjectResult result = task.getResult(); // block and wait for results to return
Copy the code
  • Upload a file

The following code is used to upload the specified local file to OSS:

// Construct the upload request.
PutObjectRequest put = new PutObjectRequest("<bucketName>"."<objectName>"."<uploadFilePath>");

// The progress callback can be set for asynchronous uploading.
put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
    @Override
    public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
        Log.d("PutObject"."currentSize: " + currentSize + " totalSize: "+ totalSize); }}); OSSAsyncTask task = oss.asyncPutObject(put,new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
    @Override
    public void onSuccess(PutObjectRequest request, PutObjectResult result) {
        Log.d("PutObject"."UploadSuccess");
        Log.d("ETag", result.getETag());
        Log.d("RequestId", result.getRequestId());
    }

    @Override
    public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
        // The request is abnormal.
        if(clientExcepion ! =null) {
            // A local exception, such as a network exception.
            clientExcepion.printStackTrace();
        }
        if(serviceException ! =null) {
            // The service is abnormal.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage()); }}});// task.cancel(); // You can cancel the task.
// task.waitUntilFinished(); // Wait for the upload to complete.
Copy the code
  • Downloading specified files

The following code is used to download the specified OSS file to a local file:

// Construct the file download request.
GetObjectRequest get = new GetObjectRequest("<bucketName>"."<objectName>");

OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
    @Override
    public void onSuccess(GetObjectRequest request, GetObjectResult result) {
        // The request succeeded.
        Log.d("asyncGetObject"."DownloadSuccess");
        Log.d("Content-Length"."" + result.getContentLength());

        InputStream inputStream = result.getObjectContent();
        byte[] buffer = new byte[2048];
        int len;

        try {
            while((len = inputStream.read(buffer)) ! = -1) {
                // You can write code here to handle the downloaded data.}}catch(IOException e) { e.printStackTrace(); }}@Override
    // Successful GetObject request returns GetObjectResult, which holds an instance of the input stream. Input stream returned, handle it yourself.
    public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
        // The request is abnormal.
        if(clientExcepion ! =null) {
            // A local exception, such as a network exception.
            clientExcepion.printStackTrace();
        }
        if(serviceException ! =null) {
            // The service is abnormal.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage()); }}});// task.cancel(); // You can cancel the task.
// task.waitUntilFinished(); // Wait for the task to complete.
Copy the code

Upload a file

  • Fragment Upload Process
  1. Initialize a fragment upload event.

Calling the oss. InitMultipartUpload method returns the globally unique uploadId created by OSS.

  1. Upload fragments.

Call the oss.uploadPart method to upload fragmented data.

  1. The fragment upload is complete.

After the completion of the upload all divided, call oss.Com pleteMultipartUpload method to merge all divided into complete file.

  • Fragmented upload complete example
// Initialize the fragment upload.
/ / InitiateMultipartUploadRequest is used to specify the upload file name and the name of the storage space of uploaded files.
//objectKey is the same as objectName, indicating that the full path containing the file suffix must be specified when uploading the file to OSS, for example, ABC /efg/123.jpg.
InitiateMultipartUploadRequest init = new InitiateMultipartUploadRequest("<bucketName>"."<objectKey>");
InitiateMultipartUploadResult initResult = oss.initMultipartUpload(init);
//initResult returns a result that contains UploadId, which uniquely identifies the Multipart Upload event.
String uploadId = initResult.getUploadId();

// Set the size of a single Part, expressed in Byte. The value ranges from 100 KB to 5 GB.
int partCount = 100*1024;
// Fragment upload.
for (int i = 1; i < 5; i++) {
    byte[] data = new byte[partCount];

    RandomAccessFile raf = new RandomAccessFile("path"."r");
    long skip = (i-1) * partCount;
    raf.seek(skip);
    raf.readFully(data, 0, partCount);

    UploadPartRequest uploadPart = new UploadPartRequest();
    uploadPart.setBucketName(mBucketName);
    uploadPart.setObjectKey(objectKey);
    uploadPart.setUploadId(uploadId);
    uploadPart.setPartNumber(i); // The uploaded Part corresponds to the PartNumber, identified from 1.
    uploadPart.setPartContent(data);
    try {
        oss.uploadPart(uploadPart);
    } catch(ServiceException serviceException) { OSSLog.logError(serviceException.getErrorCode()); }}// The fragment upload is complete.
CompleteMultipartUploadRequest complete = new CompleteMultipartUploadRequest("<bucketName>"."<objectName>"."<uploadId>"."<partETagList>";

// call back. When the fragment upload request is complete, you can set the CALLBACK_SERVER parameter. After the request is complete, the system sends a callback request to the specified Server Address. By return the result of completeResult. GetServerCallbackReturnBody view servercallback results ().
complete.setCallbackParam(new HashMap<String, String>() {
    {
        put("callbackUrl", CALLBACK_SERVER); // Change to your server address.
        put("callbackBody"."test"); }}); CompleteMultipartUploadResult completeResult = oss.completeMultipartUpload(complete); OSSLog.logError("-------------- serverCallback: " + completeResult.getServerCallbackReturnBody());
Copy the code

Netease Yunxin IM

Rainbow soft

Face Recognition Engine Package based on Hongsoft (.dll)

Github.com/jnetken/fac…

Rainbow soft face recognition C++ demo (vs)

Blog.csdn.net/zzzzjh/arti…

Hongsoft face recognition C++ SDK 2.0 secondary development

Blog.csdn.net/weixin_4433…

Rainbow soft face recognition Android Demo –1:N comparison implementation

Github.com/Sanqi5401/A…

Rainbow soft face recognition C# demo – camera multiple face 100,000 feature library

Ai.arcsoft.com.cn/bbs/forum.p…

Baidu’s eagle eye