阿里oss文件上传

开发阶段用的是js直传,没有使用sts.

在阿里客户端设置好 accessKeyIdaccessKeySecretbucket

解决跨域,进行bucket设置

  • 将allowed origins设置成 *
  • 将allowed methods设置成 PUT, GET, POST, DELETE, HEAD
  • 将allowed headers设置成 *
  • 将expose headers设置成 ETag x-oss-request-id

 

跨域未设置会报403错误。

特别注意最后一步的头部信息暴露,忘记设置的话分片传输会失败。

 

OSS的错误码参考

https://www.alibabacloud.com/help/zh/doc-detail/32005.htm

 

 

html引用文件

<scriptsrc="http://gosspublic.alicdn.com/aliyun-oss-sdk-4.4.4.min.js"></script>

html

<div>
    <input  class="newFile"  type="file" data-class="newFreePlayContent"/>
    <input type="hidden" name="newFreePlayContent" value="" class="newResourceId" data-name="" data-type=""/>
</div>

js

 

$(".newFile").on('change', function( e ){
    //e.currentTarget.files 是一个数组,如果支持多个文件,则需要遍历
    var inputElement=e.currentTarget;
    var className=$(this).attr("data-class");
    var name = e.currentTarget.files[0].name;
    //console.log(name);
    $(inputElement).parents("div").find("input[name='"+className+"']").attr("data-name",name);

    var dataType=name.split(".")[1];
    $(inputElement).parents("div").find("input[name='"+className+"']").attr("data-type",dataType);

    var file = e.currentTarget.files[0];
    var storeAs = e.currentTarget.value;

    var client = new OSS.Wrapper({
        region: '********',
        accessKeyId: '********',
        accessKeySecret: '**********',
        bucket: '**********'
    });
    client.multipartUpload(storeAs, file).then(function (result){
        //console.log(result);//返回对象
        //console.log(result.url); //返回链接
        if(result.url == undefined){
            //图片的链接会直接返回,音视频的链接会嵌套的更深
            result.url=result.res.requestUrls[0].split('?')[0];
        }
        $(inputElement).parents("div").find("input[name='"+className+"']").val(result.url);
        parent.layer.msg("文件上传成功!");
    }).catch(function (err) {
        console.log(err);
        parent.layer.msg("文件上传失败,请重试!");
    });
});

 

 

参考信息

 

上传Object后如何获取访问URL

https://help.aliyun.com/knowledge_detail/39607.html

 

搭建STS Server 并从客户端获取临时授权信息

https://help.aliyun.com/document_detail/32069.html

 

JavaScript客户端签名直传

https://help.aliyun.com/document_detail/31925.html?spm=a2c4e.11155507.0.0.LYArFT

 

你可以使用阿里云官方提供的 OSS(对象存储服务)SDK 来实现 Android 上的文件上传功能。以下是一个简单的示例代码,需要先引入阿里OSS SDK 的依赖: ```java implementation 'com.aliyun.dpa:oss-android-sdk:3.1.8' ``` 然后,在你的代码中使用以下方法来实现文件上传: ```java import com.aliyun.dpa.oss.app.AppServiceFactory; import com.aliyun.dpa.oss.app.OSSClientFactory; import com.aliyun.dpa.oss.app.RunningConfig; import com.aliyun.dpa.oss.app.ServiceFactory; import com.aliyun.dpa.oss.app.UploadConfiguration; import com.aliyun.dpa.oss.app.UploadConfigurationFactory; import com.aliyun.dpa.oss.app.UploadManager; import com.aliyun.dpa.oss.app.util.FileUtil; import com.aliyun.dpa.oss.app.util.StringUtil; import com.aliyun.dpa.oss.app.util.UIUtil; import com.aliyun.dpa.oss.callback.ResultCallback; import com.aliyun.dpa.oss.model.OSSUploadConfiguration; import com.aliyun.dpa.oss.task.PutObjectTask; import com.aliyun.dpa.oss.task.TaskCancelledException; public class AliOSSUploader { private static final String ENDPOINT = "your_oss_endpoint"; // 你的 OSS Endpoint private static final String ACCESS_KEY_ID = "your_access_key_id"; // 你的 Access Key ID private static final String ACCESS_KEY_SECRET = "your_access_key_secret"; // 你的 Access Key Secret private static final String BUCKET_NAME = "your_bucket_name"; // 你的 Bucket 名称 public void uploadFile(String filePath) { // 创建 OSS 客户端 RunningConfig runningConfig = new RunningConfig(ACCESS_KEY_ID, ACCESS_KEY_SECRET, ENDPOINT); AppServiceFactory.init(runningConfig); ServiceFactory serviceFactory = OSSClientFactory.createServiceFactory(); UploadConfiguration configuration = UploadConfigurationFactory.getUploadConfiguration(); UploadManager uploadManager = serviceFactory.createUploadManager(configuration); // 构造上传任务 PutObjectTask putObjectTask = new PutObjectTask(BUCKET_NAME, StringUtil.generateRandomKey(), filePath); // 设置上传结果回调 putObjectTask.setResultCallback(new ResultCallback() { @Override public void onSuccess(Object object) { // 文件上传成功 UIUtil.showToast("文件上传成功"); } @Override public void onFailure(Exception e) { // 文件上传失败 UIUtil.showToast("文件上传失败:" + e.getMessage()); } @Override public void onProgress(Object object, long currentSize, long totalSize) { // 文件上传进度 int progress = (int) (currentSize * 100 / totalSize); UIUtil.showToast("文件上传进度:" + progress + "%"); } @Override public void onCancel(Object object) { // 文件上传取消 UIUtil.showToast("文件上传取消"); } }); // 开始上传任务 try { uploadManager.upload(putObjectTask); } catch (TaskCancelledException e) { // 上传任务取消 UIUtil.showToast("文件上传取消"); } } } ``` 以上代码中的 `your_oss_endpoint`、`your_access_key_id`、`your_access_key_secret` 和 `your_bucket_name` 分别是你的 OSS Endpoint、Access Key ID、Access Key Secret 和 Bucket 名称。你需要将它们替换为你自己的实际值。 调用 `uploadFile` 方法并传入要上传的文件路径,即可实现文件上传阿里OSS。记得在 AndroidManifest.xml 中添加网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 希望对你有帮助!如有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端开发_穆金秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值