object.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // const debug = require('debug')('ali-oss:object');
  2. const fs = require('fs');
  3. const copy = require('copy-to');
  4. const path = require('path');
  5. const mime = require('mime');
  6. const callback = require('../common/callback');
  7. const merge = require('merge-descriptors');
  8. const { isBlob } = require('../common/utils/isBlob');
  9. const { isFile } = require('../common/utils/isFile');
  10. const { isBuffer } = require('../common/utils/isBuffer');
  11. const { obj2xml } = require('../common/utils/obj2xml');
  12. // var assert = require('assert');
  13. const proto = exports;
  14. /**
  15. * Object operations
  16. */
  17. /**
  18. * append an object from String(file path)/Buffer/ReadableStream
  19. * @param {String} name the object key
  20. * @param {Mixed} file String(file path)/Buffer/ReadableStream
  21. * @param {Object} options
  22. * @return {Object}
  23. */
  24. proto.append = async function append(name, file, options) {
  25. options = options || {};
  26. if (options.position === undefined) options.position = '0';
  27. options.subres = {
  28. append: '',
  29. position: options.position
  30. };
  31. options.method = 'POST';
  32. const result = await this.put(name, file, options);
  33. result.nextAppendPosition = result.res.headers['x-oss-next-append-position'];
  34. return result;
  35. };
  36. /**
  37. * put an object from String(file path)/Buffer/ReadableStream
  38. * @param {String} name the object key
  39. * @param {Mixed} file String(file path)/Buffer/ReadableStream
  40. * @param {Object} options
  41. * {Object} options.callback The callback parameter is composed of a JSON string encoded in Base64
  42. * {String} options.callback.url the OSS sends a callback request to this URL
  43. * {String} options.callback.host The host header value for initiating callback requests
  44. * {String} options.callback.body The value of the request body when a callback is initiated
  45. * {String} options.callback.contentType The Content-Type of the callback requests initiatiated
  46. * {Object} options.callback.customValue Custom parameters are a map of key-values, e.g:
  47. * customValue = {
  48. * key1: 'value1',
  49. * key2: 'value2'
  50. * }
  51. * @return {Object}
  52. */
  53. proto.put = async function put(name, file, options) {
  54. let content;
  55. options = options || {};
  56. options.disabledMD5 = options.disabledMD5 === undefined ? true : !!options.disabledMD5;
  57. options.headers = options.headers || {};
  58. name = this._objectName(name);
  59. if (isBuffer(file)) {
  60. content = file;
  61. } else if (isBlob(file) || isFile(file)) {
  62. if (!options.mime) {
  63. if (isFile(file)) {
  64. options.mime = mime.getType(path.extname(file.name));
  65. } else {
  66. options.mime = file.type;
  67. }
  68. }
  69. content = await this._createBuffer(file, 0, file.size);
  70. options.contentLength = await this._getFileSize(file);
  71. } else {
  72. throw new TypeError('Must provide Buffer/Blob/File for put.');
  73. }
  74. this._convertMetaToHeaders(options.meta, options.headers);
  75. const method = options.method || 'PUT';
  76. const params = this._objectRequestParams(method, name, options);
  77. callback.encodeCallback(params, options);
  78. params.mime = options.mime;
  79. params.disabledMD5 = options.disabledMD5;
  80. params.content = content;
  81. params.successStatuses = [200];
  82. const result = await this.request(params);
  83. const ret = {
  84. name,
  85. url: this._objectUrl(name),
  86. res: result.res
  87. };
  88. if (params.headers && params.headers['x-oss-callback']) {
  89. ret.data = JSON.parse(result.data.toString());
  90. }
  91. return ret;
  92. };
  93. /**
  94. * put an object from ReadableStream. If `options.contentLength` is
  95. * not provided, chunked encoding is used.
  96. * @param {String} name the object key
  97. * @param {Readable} stream the ReadableStream
  98. * @param {Object} options
  99. * @return {Object}
  100. */
  101. proto.putStream = async function putStream(name, stream, options) {
  102. options = options || {};
  103. options.headers = options.headers || {};
  104. name = this._objectName(name);
  105. if (options.contentLength) {
  106. options.headers['Content-Length'] = options.contentLength;
  107. } else {
  108. options.headers['Transfer-Encoding'] = 'chunked';
  109. }
  110. this._convertMetaToHeaders(options.meta, options.headers);
  111. const method = options.method || 'PUT';
  112. const params = this._objectRequestParams(method, name, options);
  113. callback.encodeCallback(params, options);
  114. params.mime = options.mime;
  115. params.stream = stream;
  116. params.successStatuses = [200];
  117. const result = await this.request(params);
  118. const ret = {
  119. name,
  120. url: this._objectUrl(name),
  121. res: result.res
  122. };
  123. if (params.headers && params.headers['x-oss-callback']) {
  124. ret.data = JSON.parse(result.data.toString());
  125. }
  126. return ret;
  127. };
  128. merge(proto, require('../common/object/copyObject'));
  129. merge(proto, require('../common/object/getObjectTagging'));
  130. merge(proto, require('../common/object/putObjectTagging'));
  131. merge(proto, require('../common/object/deleteObjectTagging'));
  132. merge(proto, require('../common/image'));
  133. merge(proto, require('../common/object/getBucketVersions'));
  134. merge(proto, require('../common/object/getACL'));
  135. merge(proto, require('../common/object/putACL'));
  136. merge(proto, require('../common/object/head'));
  137. merge(proto, require('../common/object/delete'));
  138. merge(proto, require('../common/object/get'));
  139. merge(proto, require('../common/object/putSymlink'));
  140. merge(proto, require('../common/object/getSymlink'));
  141. merge(proto, require('../common/object/deleteMulti'));
  142. merge(proto, require('../common/object/getObjectMeta'));
  143. merge(proto, require('../common/object/getObjectUrl'));
  144. merge(proto, require('../common/object/generateObjectUrl'));
  145. merge(proto, require('../common/object/signatureUrl'));
  146. merge(proto, require('../common/object/asyncSignatureUrl'));
  147. proto.putMeta = async function putMeta(name, meta, options) {
  148. const copyResult = await this.copy(name, name, {
  149. meta: meta || {},
  150. timeout: options && options.timeout,
  151. ctx: options && options.ctx
  152. });
  153. return copyResult;
  154. };
  155. proto.list = async function list(query, options) {
  156. // prefix, marker, max-keys, delimiter
  157. const params = this._objectRequestParams('GET', '', options);
  158. params.query = query;
  159. params.xmlResponse = true;
  160. params.successStatuses = [200];
  161. const result = await this.request(params);
  162. let objects = result.data.Contents || [];
  163. const that = this;
  164. if (objects) {
  165. if (!Array.isArray(objects)) {
  166. objects = [objects];
  167. }
  168. objects = objects.map(obj => ({
  169. name: obj.Key,
  170. url: that._objectUrl(obj.Key),
  171. lastModified: obj.LastModified,
  172. etag: obj.ETag,
  173. type: obj.Type,
  174. size: Number(obj.Size),
  175. storageClass: obj.StorageClass,
  176. owner: {
  177. id: obj.Owner.ID,
  178. displayName: obj.Owner.DisplayName
  179. }
  180. }));
  181. }
  182. let prefixes = result.data.CommonPrefixes || null;
  183. if (prefixes) {
  184. if (!Array.isArray(prefixes)) {
  185. prefixes = [prefixes];
  186. }
  187. prefixes = prefixes.map(item => item.Prefix);
  188. }
  189. return {
  190. res: result.res,
  191. objects,
  192. prefixes,
  193. nextMarker: result.data.NextMarker || null,
  194. isTruncated: result.data.IsTruncated === 'true'
  195. };
  196. };
  197. proto.listV2 = async function listV2(query, options = {}) {
  198. const continuation_token = query['continuation-token'] || query.continuationToken;
  199. if (continuation_token) {
  200. options.subres = Object.assign(
  201. {
  202. 'continuation-token': continuation_token
  203. },
  204. options.subres
  205. );
  206. }
  207. const params = this._objectRequestParams('GET', '', options);
  208. params.query = Object.assign({ 'list-type': 2 }, query);
  209. delete params.query['continuation-token'];
  210. delete params.query.continuationToken;
  211. params.xmlResponse = true;
  212. params.successStatuses = [200];
  213. const result = await this.request(params);
  214. let objects = result.data.Contents || [];
  215. const that = this;
  216. if (objects) {
  217. if (!Array.isArray(objects)) {
  218. objects = [objects];
  219. }
  220. objects = objects.map(obj => {
  221. let owner = null;
  222. if (obj.Owner) {
  223. owner = {
  224. id: obj.Owner.ID,
  225. displayName: obj.Owner.DisplayName
  226. };
  227. }
  228. return {
  229. name: obj.Key,
  230. url: that._objectUrl(obj.Key),
  231. lastModified: obj.LastModified,
  232. etag: obj.ETag,
  233. type: obj.Type,
  234. size: Number(obj.Size),
  235. storageClass: obj.StorageClass,
  236. owner
  237. };
  238. });
  239. }
  240. let prefixes = result.data.CommonPrefixes || null;
  241. if (prefixes) {
  242. if (!Array.isArray(prefixes)) {
  243. prefixes = [prefixes];
  244. }
  245. prefixes = prefixes.map(item => item.Prefix);
  246. }
  247. return {
  248. res: result.res,
  249. objects,
  250. prefixes,
  251. isTruncated: result.data.IsTruncated === 'true',
  252. keyCount: +result.data.KeyCount,
  253. continuationToken: result.data.ContinuationToken || null,
  254. nextContinuationToken: result.data.NextContinuationToken || null
  255. };
  256. };
  257. /**
  258. * Restore Object
  259. * @param {String} name the object key
  260. * @param {Object} options
  261. * @returns {{res}}
  262. */
  263. proto.restore = async function restore(name, options = { type: 'Archive' }) {
  264. options = options || {};
  265. options.subres = Object.assign({ restore: '' }, options.subres);
  266. if (options.versionId) {
  267. options.subres.versionId = options.versionId;
  268. }
  269. const params = this._objectRequestParams('POST', name, options);
  270. if (options.type === 'ColdArchive') {
  271. const paramsXMLObj = {
  272. RestoreRequest: {
  273. Days: options.Days ? options.Days : 2,
  274. JobParameters: {
  275. Tier: options.JobParameters ? options.JobParameters : 'Standard'
  276. }
  277. }
  278. };
  279. params.content = obj2xml(paramsXMLObj, {
  280. headers: true
  281. });
  282. params.mime = 'xml';
  283. }
  284. params.successStatuses = [202];
  285. const result = await this.request(params);
  286. return {
  287. res: result.res
  288. };
  289. };
  290. proto._objectUrl = function _objectUrl(name) {
  291. return this._getReqUrl({ bucket: this.options.bucket, object: name });
  292. };
  293. /**
  294. * generator request params
  295. * @return {Object} params
  296. *
  297. * @api private
  298. */
  299. proto._objectRequestParams = function _objectRequestParams(method, name, options) {
  300. if (!this.options.bucket && !this.options.cname) {
  301. throw new Error('Please create a bucket first');
  302. }
  303. options = options || {};
  304. name = this._objectName(name);
  305. const params = {
  306. object: name,
  307. bucket: this.options.bucket,
  308. method,
  309. subres: options && options.subres,
  310. timeout: options && options.timeout,
  311. ctx: options && options.ctx
  312. };
  313. if (options.headers) {
  314. params.headers = {};
  315. copy(options.headers).to(params.headers);
  316. }
  317. return params;
  318. };
  319. proto._objectName = function _objectName(name) {
  320. return name.replace(/^\/+/, '');
  321. };
  322. proto._convertMetaToHeaders = function _convertMetaToHeaders(meta, headers) {
  323. if (!meta) {
  324. return;
  325. }
  326. Object.keys(meta).forEach(k => {
  327. headers[`x-oss-meta-${k}`] = meta[k];
  328. });
  329. };
  330. proto._deleteFileSafe = function _deleteFileSafe(filepath) {
  331. return new Promise(resolve => {
  332. fs.exists(filepath, exists => {
  333. if (!exists) {
  334. resolve();
  335. } else {
  336. fs.unlink(filepath, err => {
  337. if (err) {
  338. this.debug('unlink %j error: %s', filepath, err, 'error');
  339. }
  340. resolve();
  341. });
  342. }
  343. });
  344. });
  345. };