【OSS】文件(实际就是Object)上传下载操作

package com.ls;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;

public class OSSObjectSample {

	private static final String ACCESS_ID = "*************";
	private static final String ACCESS_KEY = "*********************";
	private static final String OSS_ENDPOINT = "http://oss.aliyuncs.com/";

	public static void main(String[] args) {
		//Buceket名称
		String bucketName = "saas01";
		
		//Object对象
		String key = "A/B/C/女帝.jpg";
		/*说明:
		 * 1、要满足命名规范
		 * 2、可以制定到bucket的文件夹
		 * 3、例如A/B/C/女帝.jpg则是将文件上传到bucket的文件夹A下面的文件夹B下面的文件夹C中,命名为女帝.jpg
		 * */

		String uploadFilePath = "G:/test/女帝.jpg";
		String downloadFilePath = "G:/test/photo1.jpg";

		// 使用默认的OSS服务器地址创建OSSClient对象。
		OSSClient client = new OSSClient(OSS_ENDPOINT, ACCESS_ID, ACCESS_KEY);

		client.deleteObject(bucketName, key);

		try {
			System.out.println("正在上传...");
			
			long startTime = System.currentTimeMillis();
			uploadFile(client, bucketName, key, uploadFilePath);
			long endTime = System.currentTimeMillis();
			System.out.println("上传花费时间约:" + (endTime - startTime) + " ms");

			System.out.println("正在下载...");
			long startTime_d = System.currentTimeMillis();
			downloadFile(client, bucketName, key, downloadFilePath);
			long endTime_d = System.currentTimeMillis();
			System.out.println("下载花费时间约:" + (endTime_d - startTime_d) + " ms");
			
		} catch (OSSException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("OK");
	}

	// 上传文件
	private static void uploadFile(OSSClient client, String bucketName,
			String key, String filename) throws OSSException, ClientException,
			FileNotFoundException {
		File file = new File(filename);

		ObjectMetadata objectMeta = new ObjectMetadata();
		objectMeta.setContentLength(file.length());

		InputStream input = new FileInputStream(file);
		PutObjectResult rs = client.putObject(bucketName, key, input,
				objectMeta);
		System.out.println("上传成功:" + rs.getETag());
	}

	// 下载文件
	private static void downloadFile(OSSClient client, String bucketName,
			String key, String filename) throws OSSException, ClientException {
		client.getObject(new GetObjectRequest(bucketName, key), new File(
				filename));
	}

}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要将OSS文件上传到OpenStack Glance,你需要使用阿里云OSS SDK下载文件,并使用OpenStack Glance的API将文件上传为镜像。以下是一个示例代码,演示了如何使用阿里云OSS Go SDK和OpenStack Go SDK完成此操作: ```go package main import ( "fmt" "io/ioutil" "os" "github.com/aliyun/aliyun-oss-go-sdk/oss" "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/auth/aksk" "github.com/gophercloud/gophercloud/openstack" "github.com/gophercloud/gophercloud/openstack/imageservice/v2/images" ) func main() { // 配置阿里云OSS连接 ossEndpoint := "your-oss-endpoint" ossAccessKey := "your-oss-access-key" ossAccessSecret := "your-oss-access-secret" bucketName := "your-bucket-name" objectKey := "your-object-key" // 创建OSS客户端 client, err := oss.New(ossEndpoint, ossAccessKey, ossAccessSecret) if err != nil { fmt.Println("Failed to create OSS client: ", err) return } // 从OSS下载镜像文件 tempFile, err := ioutil.TempFile("", "image") if err != nil { fmt.Println("Failed to create temporary file: ", err) return } defer os.Remove(tempFile.Name()) err = client.GetObjectToFile(bucketName, objectKey, tempFile.Name()) if err != nil { fmt.Println("Failed to download image from OSS: ", err) return } // 配置OpenStack连接 openStackAuthURL := "your-openstack-auth-url" openStackUsername := "your-openstack-username" openStackPassword := "your-openstack-password" openStackTenantID := "your-openstack-tenant-id" opts := gophercloud.AuthOptions{ IdentityEndpoint: openStackAuthURL, Username: openStackUsername, Password: openStackPassword, TenantID: openStackTenantID, } provider, err := openstack.AuthenticatedClient(opts) if err != nil { fmt.Println("Failed to authenticate with OpenStack: ", err) return } // 创建OpenStack Image Service客户端 imageClient, err := openstack.NewImageServiceV2(provider, gophercloud.EndpointOpts{}) if err != nil { fmt.Println("Failed to create OpenStack Image Service client: ", err) return } // 创建Glance镜像 image, err := images.Create(imageClient, images.CreateOpts{ Name: "imported-image", DiskFormat: "qcow2", ContainerFormat: "bare", Visibility: images.Public, }).Extract() if err != nil { fmt.Println("Failed to create Glance image: ", err) return } // 上传镜像到Glance imageData, err := os.Open(tempFile.Name()) if err != nil { fmt.Println("Failed to open image file: ", err) return } defer imageData.Close() err = images.Upload(imageClient, image.ID, imageData).ExtractErr() if err != nil { fmt.Println("Failed to upload image to Glance: ", err) return } fmt.Println("Image upload completed successfully!") } ``` 确保替换示例代码中的"your-oss-endpoint"、"your-oss-access-key"、"your-oss-access-secret"、"your-bucket-name"、"your-object-key"、"your-openstack-auth-url"、"your-openstack-username"、"your-openstack-password"和"your-openstack-tenant-id"参数为你的实际值。 这段示例代码中使用了阿里云OSS Go SDK和OpenStack Go SDK来实现从OSS下载文件并将其上传到OpenStack Glance。首先,它通过阿里云OSS Go SDK从OSS下载镜像文件到本地临时文件。然后,通过OpenStack Go SDK创建Glance镜像,并使用Glance API将镜像文件上传到Glance。 请确保你已经安装了所需的Go SDK并正确配置了相关的认证信息和权限。如有任何疑问,请参考相关SDK的文档或与社区进行进一步交流。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值